Advertisement
_no0B

Untitled

Nov 1st, 2021
889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int dis[1005][1005];
  6.  
  7. string str[1005];
  8.  
  9. const string order = "DLRU";
  10.  
  11. int dxx[] = {1,0,0,-1};
  12. int dyy[] = {0,-1,1,0};
  13.  
  14. void bfs(int row , int col , int n , int m)
  15. {
  16.     memset(dis,-1,sizeof dis);
  17.     queue < pair < int , int > > que;
  18.     que.push({row,col});
  19.     dis[row][col] = 0;
  20.     while(!que.empty()){
  21.         row = que.front().first , col = que.front().second;
  22.         que.pop();
  23.         for(int i = 0 ; i < 4 ; i++){
  24.             int x = row + dxx[i]*(str[row][col] - '0') , y = col + dyy[i]*(str[row][col] - '0');
  25.             if(min(x,y) >= 0 && x < n && y < m && dis[x][y] == -1){
  26.                 dis[x][y] = dis[row][col] + 1;
  27.                 que.push({x,y});
  28.             }
  29.         }
  30.     }
  31.  
  32. }
  33.  
  34.  
  35. int main()
  36. {
  37.     /// problem F
  38.     int n , m ;
  39.     cin>>n>>m;
  40.     for(int i = 0 ; i < n ; i++){
  41.         cin>>str[i];
  42.  
  43.     }
  44.  
  45.     bfs(0,0,n,m);
  46.     cout<<dis[n-1][m-1]<<endl;
  47.     return 0;
  48. }
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement