Advertisement
SorahISA

E. 方格迷宮 (maze)

Oct 5th, 2022
715
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int maxn = 100 + 5;
  5.  
  6. const pair<int, int> dir[4] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
  7.  
  8. int val[maxn][maxn], dis[maxn][maxn];
  9.  
  10. void bfs(int sx, int sy, int bit) {
  11.     memset(dis, 0x3F, sizeof(dis)); /// INF
  12.     if (val[sx][sy] >> bit & 1) return; /// can't even start
  13.     dis[sx][sy] = 0;
  14.     queue<pair<int, int>> que; que.emplace(sx, sy);
  15.     while (!que.empty()) {
  16.         auto [nx, ny] = que.front(); que.pop();
  17.         /// only push if can update dis ///
  18.         /// since using bfs, dis will never be updated twice ///
  19.         for (auto [dx, dy] : dir) {
  20.             if (dis[nx+dx][ny+dy] != dis[0][0] or ((val[nx+dx][ny+dy] >> bit) & 1)) continue;
  21.             dis[nx+dx][ny+dy] = dis[nx][ny] + 1;
  22.             que.emplace(nx+dx, ny+dy);
  23.         }
  24.     }
  25. }
  26.  
  27. void solve() {
  28.     int N, M; cin >> N >> M;
  29.    
  30.     memset(val, 0xFF, sizeof(val)); /// -1, set all bits to 1
  31.     for (int i = 1; i <= N; ++i) {
  32.         for (int j = 1; j <= M; ++j) cin >> val[i][j];
  33.     }
  34.    
  35.     int x0, y0, x1, y1; cin >> x0 >> y0 >> x1 >> y1;
  36.    
  37.     int ans = 0;
  38.     for (int bit = 29; bit >= 0; --bit) {
  39.         bfs(x0, y0, bit);
  40.         if (dis[x1][y1] != dis[0][0]) { /// dis[0][0] is always INF
  41.             /// if (x0, y0) is able to walk to (x1, y1) without having `bit`-th bit be 1 ///
  42.             /// then we can just discard those grid forever (by setting every bit to 1) ///
  43.             for (int i = 1; i <= N; ++i) {
  44.                 for (int j = 1; j <= M; ++j) {
  45.                     if (dis[i][j] == dis[0][0]) val[i][j] = val[0][0]; /// val[0][0] is always all 1
  46.                 }
  47.             }
  48.         }
  49.         else {
  50.             /// must have `bit`-th bit be 1 ///
  51.             ans |= (1 << bit);
  52.         }
  53.     }
  54.     bfs(x0, y0, 31); /// can walk if not discarded
  55.    
  56.     /// back-tracking to find answer ///
  57.     string command = "";
  58.     int nx = x1, ny = y1;
  59.     while (nx != x0 or ny != y0) {
  60.              if (dis[nx-1][ny] < dis[nx][ny]) --nx, command += "D";
  61.         else if (dis[nx+1][ny] < dis[nx][ny]) ++nx, command += "U";
  62.         else if (dis[nx][ny-1] < dis[nx][ny]) --ny, command += "R";
  63.         else if (dis[nx][ny+1] < dis[nx][ny]) ++ny, command += "L";
  64.         else exit(-1); /// why here ???
  65.     }
  66.     reverse(begin(command), end(command));
  67.    
  68.     cout << ans << " " << dis[x1][y1] << "\n";
  69.     cout << command << "\n";
  70. }
  71.  
  72. int main() {
  73.     ios_base::sync_with_stdio(0), cin.tie(0);
  74.    
  75.     int t; cin >> t;
  76.     for (int _ = 1; _ <= t; ++_) {
  77.         solve();
  78.     }
  79.    
  80.     return 0;
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement