Advertisement
Corezzi

UVA 314

Feb 2nd, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int left(int i){
  6.     if(i == 0) return 3;
  7.     else return i-1;
  8. }
  9.  
  10. int right(int i){
  11.     if(i == 3) return 0;
  12.     else return i+1;
  13. }
  14.  
  15. int main(){
  16.     // ios_base::sync_with_stdio(false);
  17.     // cin.tie(NULL);
  18.    
  19.     int n,m;
  20.     while(cin >> n >> m, n || m){
  21.         vector<vector<int>> gin(n, vector<int>(m));
  22.         vector<vector<int>> g(n-1, vector<int>(m-1));
  23.        
  24.         for(int i = 0; i < n; ++i){
  25.             for(int j = 0; j < m; ++j){
  26.                 cin >> gin[i][j];
  27.             }
  28.         }
  29.        
  30.         for(int i = 0; i < n-1; ++i){
  31.             for(int j = 0; j < m-1; ++j){
  32.                 if(gin[i][j] || gin[i+1][j] || gin[i][j+1] || gin[i+1][j+1])
  33.                     g[i][j] = 1;
  34.                 //cout << g[i][j] << "\t";
  35.             }//cout << endl;
  36.         }
  37.        
  38.         string dir;
  39.         int b1, b2, e1, e2, ori;
  40.         cin >> b1 >> b2 >> e1 >> e2 >> dir;
  41.         b1--;b2--;
  42.         e1--;e2--;
  43.        
  44.         if(dir[0] == 'n') ori = 0;
  45.         if(dir[0] == 'e') ori = 1;
  46.         if(dir[0] == 's') ori = 2;
  47.         if(dir[0] == 'w') ori = 3;
  48.        
  49.         struct Pos{
  50.             int x,y,dir;
  51.         };
  52.         int d[n][m][4];
  53.         memset(d, -1, sizeof(d));
  54.         queue<Pos> q;
  55.         q.push({b1,b2,ori});
  56.         d[b1][b2][ori] = 0;
  57.        
  58.         int roff[]{-1,0,1,0};
  59.         int coff[]{0,1,0,-1};
  60.         int resp = -1;
  61.        
  62.         while(!q.empty()){
  63.             auto p = q.front(); q.pop();
  64.             //cout << p.x << " " << p.y << " " << p.dir << endl;
  65.             if(p.x == e1 && p.y == e2){
  66.                 resp = d[p.x][p.y][p.dir];
  67.                 break;
  68.             }
  69.            
  70.             if(d[p.x][p.y][right(p.dir)] == -1){
  71.                 d[p.x][p.y][right(p.dir)] = d[p.x][p.y][p.dir] + 1;
  72.                 q.push({p.x,p.y,right(p.dir)});
  73.             }
  74.            
  75.             if(d[p.x][p.y][left(p.dir)] == -1){
  76.                 d[p.x][p.y][left(p.dir)] = d[p.x][p.y][p.dir] + 1;
  77.                 q.push({p.x,p.y,left(p.dir)});
  78.             }
  79.            
  80.             for(int i = 1; i <= 3; ++i){
  81.                 int r = p.x + roff[p.dir]*i;
  82.                 int c = p.y + coff[p.dir]*i;
  83.                
  84.                 if(r >= 0 && r < n-1 && c >= 0 && c < m-1){
  85.                     if(g[r][c]) break;
  86.                     if(d[r][c][p.dir] == -1){
  87.                         d[r][c][p.dir] = d[p.x][p.y][p.dir] + 1;
  88.                         q.push({r,c,p.dir});
  89.                     }
  90.                 }
  91.             }
  92.         }
  93.         cout << resp << endl;
  94.         // for(int k = 0; k < 4; ++k){
  95.             // for(int i = 0; i < n-1; ++i){
  96.                 // for(int j = 0; j < m-1; ++j){
  97.                     // cout << d[i][j][k] << "\t";
  98.                 // }cout << endl;
  99.             // }
  100.             // cout << endl;
  101.         // }
  102.     }
  103.    
  104.     return 0;
  105.    
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement