Advertisement
Guest User

lab

a guest
Nov 20th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <string>
  4. using namespace std;
  5.  
  6. struct pkt{
  7.     int x; int y;
  8. };
  9.  
  10. int w, h, xP, yP, xW, yW, odl[1002][1002];
  11. //int w, h;
  12. string rows[1002], pom;
  13. bool odw[1002][1002];
  14. queue <pkt>q;
  15.  
  16. void bfs()
  17. {
  18.     int x, y;
  19.     pkt p, pom;
  20.     p.x=xP; p.y=yP;
  21.     odw[yP][xP]=true;
  22.     q.push(p);
  23.     while(!q.empty())
  24.     {
  25.         if(rows[p.y][p.x]=='W') break;
  26.         p.x=q.front().x; p.y=q.front().y;
  27.         q.pop();
  28.         //cout <<p.x<<" "<<p.y<<endl;
  29.         if(rows[p.y+1][p.x]!='#'&&odw[p.y+1][p.x]==false)
  30.         {
  31.             pom.y=p.y+1; pom.x=p.x;
  32.             q.push(pom);
  33.             odw[p.y+1][p.x]=true;
  34.             odl[p.y+1][p.x]=odl[p.y][p.x]+1;
  35.         }
  36.         if(rows[p.y][p.x+1]!='#'&&odw[p.y][p.x+1]==false)
  37.         {
  38.             pom.y=p.y; pom.x=p.x+1;
  39.             q.push(pom);
  40.             odw[p.y][p.x+1]=true;
  41.             odl[p.y][p.x+1]=odl[p.y][p.x]+1;
  42.         }
  43.         if(rows[p.y-1][p.x]!='#'&&odw[p.y-1][p.x]==false)
  44.         {
  45.             pom.y=p.y-1; pom.x=p.x;
  46.             q.push(pom);
  47.             odw[p.y-1][p.x]=true;
  48.             odl[p.y-1][p.x]=odl[p.y][p.x]+1;
  49.         }
  50.         if(rows[p.y][p.x-1]!='#'&&odw[p.y][p.x-1]==false)
  51.         {
  52.             pom.y=p.y; pom.x=p.x-1;
  53.             q.push(pom);
  54.             odw[p.y][p.x-1]=true;
  55.             odl[p.y][p.x-1]=odl[p.y][p.x]+1;
  56.         }
  57.     }
  58.     if(rows[p.y][p.x]!='W') cout <<"NIE";
  59.     //if(odl[p.y][p.x]==5) cout <<w<<"."<<h;
  60.     else if(odl[p.y][p.x]==0) cout <<"NIE";
  61.     else cout <<odl[p.y][p.x];
  62. }
  63.  
  64.  
  65. int main()
  66. {
  67.     ios_base::sync_with_stdio(false);
  68.     cin.tie(NULL);
  69.     cout.tie(NULL);
  70.     cin >>w >>h;
  71.  
  72.     for(int i=0; i<=w+1; i++) rows[0]+="#";
  73.  
  74.     for(int i=1; i<=h; i++)
  75.     {
  76.         rows[i]+="#";
  77.         cin >>pom; rows[i]+=pom;
  78.         if(pom.find("P")!=-1) {xP=pom.find("P")+1; yP=i;}
  79.         if(pom.find("W")!=-1) {xW=pom.find("W")+1; yW=i;}
  80.         //}
  81.         rows[i]+="#";
  82.     }
  83.     for(int i=0; i<=w+1; i++) rows[h+1]+="#";
  84.     cout <<endl<<w<<" "<<h<<endl;
  85.     for(int i=0; i<=h+1; i++)
  86.     {
  87.         //for(int j=0; j<=w+1; j++)
  88.         cout <<rows[i];
  89.         cout <<endl;
  90.     }
  91.     bfs();
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement