Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<queue>
- using namespace std;
- char a[1111][1111];
- int ans[1111][1111],n,m;
- void wave(int x,int y)
- {
- queue<pair<int, int> > q;
- pair<int, int> p;
- q.push(make_pair(x, y));
- while(!q.empty())
- {
- p = q.front();
- q.pop();
- x = p.first; y = p.second;
- if(x-1>=0 && a[x-1][y]!='#' && !ans[x-1][y]){
- ans[x-1][y]=ans[x][y]+1;
- q.push(make_pair(x-1, y));
- }
- if(y+1<m && a[x][y+1]!='#' && !ans[x][y+1]){
- ans[x][y+1]=ans[x][y]+1;
- q.push(make_pair(x, y+1));
- }
- if(x+1<n && a[x+1][y]!='#' && !ans[x+1][y]){
- ans[x+1][y]=ans[x][y]+1;
- q.push(make_pair(x+1, y));
- }
- if(y-1>=0 && a[x][y-1]!='#' && !ans[x][y-1]){
- ans[x][y-1]=ans[x][y]+1;
- q.push(make_pair(x, y-1));
- }
- }
- }
- int main()
- {
- int i, j,startx,starty,endx,endy;
- cin>>n>>m;
- for(i=0; i<n; i++)
- for(j=0; j<m; j++)
- {
- cin>>a[i][j];
- if(a[i][j]=='*'){startx=i;starty=j;}
- if(a[i][j]=='$'){endx=i;endy=j;}
- }
- wave(startx,starty);
- cout<<ans[endx][endy];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement