Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int dis[1005][1005];
- string str[1005];
- const string order = "DLRU";
- int dxx[] = {1,0,0,-1};
- int dyy[] = {0,-1,1,0};
- void bfs(int row , int col , int n , int m)
- {
- memset(dis,-1,sizeof dis);
- queue < pair < int , int > > que;
- que.push({row,col});
- dis[row][col] = 0;
- while(!que.empty()){
- row = que.front().first , col = que.front().second;
- que.pop();
- for(int i = 0 ; i < 4 ; i++){
- int x = row + dxx[i]*(str[row][col] - '0') , y = col + dyy[i]*(str[row][col] - '0');
- if(min(x,y) >= 0 && x < n && y < m && dis[x][y] == -1){
- dis[x][y] = dis[row][col] + 1;
- que.push({x,y});
- }
- }
- }
- }
- int main()
- {
- /// problem F
- int n , m ;
- cin>>n>>m;
- for(int i = 0 ; i < n ; i++){
- cin>>str[i];
- }
- bfs(0,0,n,m);
- cout<<dis[n-1][m-1]<<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement