Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- #include <string>
- using namespace std;
- struct pkt{
- int x; int y;
- };
- int w, h, xP, yP, xW, yW, odl[1002][1002];
- //int w, h;
- string rows[1002], pom;
- bool odw[1002][1002];
- queue <pkt>q;
- void bfs()
- {
- int x, y;
- pkt p, pom;
- p.x=xP; p.y=yP;
- odw[yP][xP]=true;
- q.push(p);
- while(!q.empty())
- {
- if(rows[p.y][p.x]=='W') break;
- p.x=q.front().x; p.y=q.front().y;
- q.pop();
- //cout <<p.x<<" "<<p.y<<endl;
- if(rows[p.y+1][p.x]!='#'&&odw[p.y+1][p.x]==false)
- {
- pom.y=p.y+1; pom.x=p.x;
- q.push(pom);
- odw[p.y+1][p.x]=true;
- odl[p.y+1][p.x]=odl[p.y][p.x]+1;
- }
- if(rows[p.y][p.x+1]!='#'&&odw[p.y][p.x+1]==false)
- {
- pom.y=p.y; pom.x=p.x+1;
- q.push(pom);
- odw[p.y][p.x+1]=true;
- odl[p.y][p.x+1]=odl[p.y][p.x]+1;
- }
- if(rows[p.y-1][p.x]!='#'&&odw[p.y-1][p.x]==false)
- {
- pom.y=p.y-1; pom.x=p.x;
- q.push(pom);
- odw[p.y-1][p.x]=true;
- odl[p.y-1][p.x]=odl[p.y][p.x]+1;
- }
- if(rows[p.y][p.x-1]!='#'&&odw[p.y][p.x-1]==false)
- {
- pom.y=p.y; pom.x=p.x-1;
- q.push(pom);
- odw[p.y][p.x-1]=true;
- odl[p.y][p.x-1]=odl[p.y][p.x]+1;
- }
- }
- if(rows[p.y][p.x]!='W') cout <<"NIE";
- //if(odl[p.y][p.x]==5) cout <<w<<"."<<h;
- else if(odl[p.y][p.x]==0) cout <<"NIE";
- else cout <<odl[p.y][p.x];
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- cout.tie(NULL);
- cin >>w >>h;
- for(int i=0; i<=w+1; i++) rows[0]+="#";
- for(int i=1; i<=h; i++)
- {
- rows[i]+="#";
- cin >>pom; rows[i]+=pom;
- if(pom.find("P")!=-1) {xP=pom.find("P")+1; yP=i;}
- if(pom.find("W")!=-1) {xW=pom.find("W")+1; yW=i;}
- //}
- rows[i]+="#";
- }
- for(int i=0; i<=w+1; i++) rows[h+1]+="#";
- cout <<endl<<w<<" "<<h<<endl;
- for(int i=0; i<=h+1; i++)
- {
- //for(int j=0; j<=w+1; j++)
- cout <<rows[i];
- cout <<endl;
- }
- bfs();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement