Facebook
From Long Vu, 2 Weeks ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 142
  1. #include <bits/stdc++.h>                                                                                                                                                                                                                                                                                           //Created by CUBERLONG
  2. using namespace std;                                                                                                                                                                                                                                                                                               //Created by CUBERLONG
  3. #define int long long
  4. void _time() {
  5.     #ifndef ONLINE_JUDGE
  6.         cout << "_nTime: " << (int)clock() << "ms.n";                                                                                                                                                                                                                                                                                           //Created by CUBERLONG
  7.     #endif
  8. }
  9. void fast() {
  10.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  11.     #ifndef ONLINE_JUDGE
  12.         freopen("ab.inp", "r", stdin);
  13.         freopen("ab.out", "w", stdout);
  14.     #endif
  15. }
  16. #define II pair<int,int>
  17. #define x first
  18. #define y second
  19. const int N = 1505;
  20. int r, c;
  21. int f[N][N];
  22. char a[N][N];
  23. bool visited[N][N];
  24. II dxy[] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
  25. vector<II> L;
  26. queue<II> qu1, qu2;
  27.  
  28. bool inside(II x) {
  29.     return 0 <= x.x && x.x < r && 0 <= x.y && x.y < c;
  30. }
  31.  
  32. void visit(II u, int len, bool next) {
  33.     len += next;
  34.     for (II d: dxy) {
  35.         II v = {u.x + d.x, u.y + d.y};
  36.         if (inside(v) && !visited[v.x][v.y] && f[v.x][v.y] <= len) {
  37.             visited[v.x][v.y] = 1;
  38.             qu1.push(v);
  39.         }
  40.     }
  41. }
  42. int bfs() {
  43.     qu1.push(L[0]);
  44.     visited[L[0].x][L[0].y] = 1;
  45.     for (int len = 0; 1; ++len) {
  46.         while (!qu1.empty()) {
  47.             II u = qu1.front(); qu1.pop();
  48.             qu2.push(u);
  49.             visit(u, len, 0);
  50.         }
  51.         while (!qu2.empty()) {
  52.             II u = qu2.front(); qu2.pop();
  53.             if (u == L[1]) return len;
  54.             visit(u, len, 1);
  55.         }
  56.     }
  57.     return -1;
  58. }
  59. signed main() {
  60.     fast();
  61.  
  62.     cin >> r >> c;
  63.     memset(f, -1, sizeof f);
  64.     queue<II> qu;
  65.     for (int i = 0; i < r; ++i)
  66.         for (int j = 0; j < c; ++j) {
  67.             cin >> a[i][j];
  68.             if (a[i][j] == 'L')
  69.                 L.push_back({i, j});
  70.             if (a[i][j] != 'X') {
  71.                 f[i][j] = 0;
  72.                 qu.push({i, j});
  73.             }
  74.         }
  75.     while (!qu.empty()) {
  76.         II u = qu.front(); qu.pop();
  77.         for (II d: dxy) {
  78.             II v = {u.x + d.x, u.y + d.y};
  79.             if (inside(v) && f[v.x][v.y] < 0) {
  80.                 f[v.x][v.y] = f[u.x][u.y] + 1;
  81.                 qu.push(v);
  82.             }
  83.         }
  84.     }
  85.     cout << bfs();
  86.  
  87.     return _time(), 0;
  88. }                                                                                                                                                                                                                                                                                                               //Created by CUBERLONG
  89. /*
  90. */
  91.