SHARE
TWEET

866

a guest Feb 17th, 2020 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cassert>
  4. using namespace std;
  5.  
  6. ifstream fin("acces.in");
  7. ofstream fout("acces.out");
  8.  
  9. int n , m;
  10. short a[1001][1001];
  11. int  x[1000001], y[1000001];
  12. char P[1001][1001];
  13. int xp , yp;
  14.  
  15.  
  16. const int dx[] = {0 , 0 , 1 , -1},
  17.           dy[] = {1 , -1 , 0 , 0};
  18.  
  19. int main(){
  20.     fin >> n >> m;
  21.     for(int i = 1 ; i <= n ; i ++)
  22.         for(int j = 1 ; j <= m ; j ++)
  23.         {
  24.             fin >> P[i][j];
  25.             if(P[i][j] == 'P')
  26.                 xp = i, yp = j;
  27.         }
  28.    
  29.     fin.close();
  30.    
  31.     int st , dr;
  32.     st = dr = 1;
  33.     x[dr] = xp, y[dr] = yp;
  34.     a[x[1]][y[1]] = 1;
  35.     while(st <= dr)
  36.     {
  37.         int i = x[st], j = y[st];
  38.         for(int k = 0 ; k < 4 ; k ++)
  39.         {
  40.             int ii = i + dx[k], jj = j + dy[k];
  41.             if( ii > 0 && ii <= n && jj > 0 && jj <= m && P[ii][jj] !='#' && a[ii][jj] == 0)
  42.             {
  43.                 a[ii][jj] = a[i][j] + 1;
  44.                 dr ++;
  45.                 x[dr] = ii, y[dr] = jj;
  46.             }
  47.         }
  48.         st ++;
  49.     }
  50.    
  51.     for(int i = 1 ; i <= n ; i ++)
  52.     {
  53.         for(int j = 1 ; j <= m ; j ++)
  54.             fout << a[i][j] - 1 << " ";
  55.         fout << "\n";
  56.     }
  57.    
  58.     fout.close();
  59.     return 0;
  60. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top