Advertisement
Patrickmeme

Muzeu

May 28th, 2023
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. #include <fstream>
  2. #include <queue>
  3. using namespace std;
  4.  
  5. ifstream cin("muzeu.in");
  6. ofstream cout("muzeu.out");
  7.  
  8. bool v[251][251];
  9. int min1[251][251];
  10.  
  11. int ml[]={1,0,-1,0};
  12. int mc[]={0,1,0,-1};
  13.  
  14. int main()
  15. {
  16.     int n,i,j,l,c,cntd;
  17.     char ch;
  18.     cin>>n;
  19.     queue<pair<int,int>>deMers;
  20.     queue<int>cnt;
  21.     for(i=1;i<=n;i++){
  22.         for(j=1;j<=n;j++){
  23.             min1[i][j]=9999999;
  24.             cin>>ch;
  25.             if(ch=='#'){
  26.                 v[i][j]=1;
  27.             }else if(ch=='P'){
  28.                 min1[i][j]=0;
  29.                 deMers.push({i,j});
  30.                 cnt.push(0);
  31.             }
  32.         }
  33.     }
  34.     while(!deMers.empty()){
  35.         pair<int,int>poz=deMers.front();
  36.         cntd=cnt.front()+1;
  37.         deMers.pop();
  38.         cnt.pop();
  39.         for(j=0;j<4;j++){
  40.             l=poz.first+ml[j];
  41.             c=poz.second+mc[j];
  42.             if(l<=n && c<=n && l>0 && c>0 && v[l][c]!=1 && cntd<min1[l][c]){
  43.                 min1[l][c]=cntd;
  44.                 cnt.push(cntd);
  45.                 deMers.push({l,c});
  46.             }
  47.         }
  48.      }
  49.  
  50.     for(i=1;i<=n;i++){
  51.         for(j=1;j<n;j++){
  52.             if(v[i][j]==1)
  53.                 cout<<"-2 ";
  54.             else if(min1[i][j]==9999999)
  55.                 cout<<"-1 " ;
  56.             else
  57.                 cout<<" "<<min1[i][j]<<" ";
  58.         }
  59.         if(v[i][j]==1)
  60.             cout<<"-2 ";
  61.         else if(min1[i][j]==9999999)
  62.             cout<<"-1 " ;
  63.         else
  64.             cout<<" "<<min1[i][j];
  65.         cout<<"\n";
  66.     }
  67.     return 0;
  68. }
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement