Advertisement
mickypinata

TOI10: Map

Jul 4th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.99 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. #define pii pair<int, int>
  7. #define f first
  8. #define s second
  9.  
  10. int main(){
  11.  
  12.     int row, col;
  13.     scanf("%d", &row);
  14.     scanf("%d", &col);
  15.     vector<vector<int>> adj(row * col, vector<int>(4, -1)); // 0=U 1=R 2=D 3=L
  16.  
  17.     int loop = row * col - 1;
  18.     for(int i = 1; i <= loop; ++i){
  19.         int u, v;
  20.         char cmd;
  21.         scanf("%d", &u);
  22.         scanf(" %c", &cmd);
  23.         scanf("%d", &v);
  24.         if(cmd == 'U'){
  25.             adj[u][2] = v;
  26.             adj[v][0] = u;
  27.         } else if(cmd == 'L'){
  28.             adj[u][1] = v;
  29.             adj[v][3] = u;
  30.         }
  31.     }
  32.  
  33.     /// BFS
  34.     int mnr = 0;
  35.     int mnc = 0;
  36.     vector<pii> dist(row * col, {0, 0});
  37.     queue<int> q;
  38.     q.push(0);
  39.     dist[0] = {0, 0};
  40.     while(!q.empty()){
  41.         int u = q.front();
  42.         q.pop();
  43.         for(int i = 0; i < 4; ++i){
  44.             int v = adj[u][i];
  45.             if(v != -1 && dist[v] == make_pair(0, 0)){
  46.                 switch(i){
  47.                 case 0: // Up
  48.                     dist[v] = {dist[u].f - 1, dist[u].s};
  49.                     break;
  50.                 case 1: // Right
  51.                     dist[v] = {dist[u].f, dist[u].s + 1};
  52.                     break;
  53.                 case 2: // Down
  54.                     dist[v] = {dist[u].f + 1, dist[u].s};
  55.                     break;
  56.                 case 3: // Left
  57.                     dist[v] = {dist[u].f, dist[u].s - 1};
  58.                     break;
  59.                 }
  60.                 mnr = min(mnr, dist[v].f);
  61.                 mnc = min(mnc, dist[v].s);
  62.                 q.push(v);
  63.             }
  64.         }
  65.     }
  66.  
  67.     vector<vector<int>> mp(row + 1, vector<int>(col + 1, 0));
  68.     for(int i = 0; i <= loop; ++i){
  69.         mp[dist[i].f - mnr][dist[i].s - mnc] = i;
  70.     }
  71.  
  72.     for(int i = 0; i < row; ++i){
  73.         for(int j = 0; j < col; ++j){
  74.             cout << mp[i][j] << " ";
  75.         }
  76.         cout << "\n";
  77.     }
  78.  
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement