Advertisement
Dang_Quan_10_Tin

DH2020_COVID19

Aug 12th, 2021
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <queue>
  4. #include <vector>
  5. #include <cstring>
  6. #define task "COVID19"
  7. using namespace std;
  8. using ll = long long;
  9. using ld = long double;
  10.  
  11. const int N = 1e6 + 3;
  12. const int Inf = 1e9 + 7;
  13. int abcdxyzt[N * 3], d[N * 3];
  14. bool ck[N * 3];
  15. int m, n, q;
  16. pair<int, int> rev[N];
  17. int x[] = {1, 0, -1, 0}, y[] = {0, 1, 0, -1};
  18. vector<int> s[N];
  19.  
  20. #define a(x, y) abcdxyzt[((x)-1) * n + (y)]
  21. #define pos(x, y) (((x)-1) * n + (y))
  22.  
  23. void Read()
  24. {
  25.     cin >> m >> n >> q;
  26.     for (int i = 1; i <= m; ++i)
  27.         for (int j = 1; j <= n; ++j)
  28.         {
  29.             cin >> a(i, j);
  30.             s[i * j].push_back(pos(i, j));
  31.             rev[pos(i, j)] = {i, j};
  32.         }
  33. }
  34.  
  35. int BFS(int xx, int yy, int z, int t)
  36. {
  37.     memset(ck, 0, sizeof ck);
  38.  
  39.     queue<int> q;
  40.     d[pos(xx, yy)] = 0;
  41.     q.emplace(pos(xx, yy));
  42.     ck[pos(xx, yy)] = 1;
  43.  
  44.     while (q.size())
  45.     {
  46.         int c = q.front();
  47.         q.pop();
  48.  
  49.         if (c == pos(z, t))
  50.             return d[c];
  51.  
  52.         if (c <= m * n)
  53.         {
  54.             for (int i = 0; i < 4; ++i)
  55.             {
  56.                 int u = rev[c].first + x[i],
  57.                     v = rev[c].second + y[i];
  58.  
  59.                 if (u && u <= m && v && v <= n && !ck[pos(u, v)])
  60.                 {
  61.                     ck[pos(u, v)] = 1;
  62.                     d[pos(u, v)] = d[c] + 1;
  63.                     q.emplace(pos(u, v));
  64.                 }
  65.             }
  66.  
  67.             if (abcdxyzt[c] <= m * n && !ck[abcdxyzt[c] + m * n])
  68.             {
  69.                 ck[abcdxyzt[c] + m * n] = 1;
  70.                 d[abcdxyzt[c] + m * n] = d[c] + 1;
  71.                 q.emplace(abcdxyzt[c] + m * n);
  72.             }
  73.         }
  74.         else if (c <= m * n * 2)
  75.         {
  76.             if (!ck[c + m * n])
  77.             {
  78.                 ck[c + m * n] = 1;
  79.                 d[c + m * n] = d[c] + 1;
  80.                 q.emplace(c + m * n);
  81.             }
  82.         }
  83.         else
  84.         {
  85.             for (auto i : s[c - m * n * 2])
  86.                 if (!ck[i])
  87.                 {
  88.                     ck[i] = 1;
  89.                     d[i] = d[c] + 1;
  90.                     q.emplace(i);
  91.                 }
  92.         }
  93.     }
  94.     return d[pos(z, t)];
  95. }
  96.  
  97. void Solve()
  98. {
  99.     int x, y, z, t;
  100.     cin >> x >> y >> z >> t;
  101.     cout << BFS(x, y, z, t) << "\n";
  102. }
  103.  
  104. int32_t main()
  105. {
  106.     ios::sync_with_stdio(0);
  107.     cin.tie(0);
  108.     cout.tie(0);
  109.     if (fopen(task ".INP", "r"))
  110.         freopen(task ".INP", "r", stdin),
  111.             freopen(task ".OUT", "w", stdout);
  112.     Read();
  113.     while (q--)
  114.         Solve();
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement