SHARE
TWEET

883

a guest Feb 17th, 2020 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. ifstream fin("traversare.in");
  6. ofstream fout("traversare.out");
  7.  
  8. int n , m, nrI, nrP;
  9. int  a[1002][1002];
  10. short x[1000005], y[1000005];
  11.  
  12. const int dx[]={0 , 0 , 1 , -1}, dy[]={1 , -1 , 0 , 0};
  13.  
  14. int main(){
  15.     fin >> n >> m;
  16.     for(int i = 1 ; i <= n ; i ++)
  17.         for(int j = 1 ; j <= m ; j ++)
  18.             fin >> a[i][j];
  19.     //facem obstacolele -1
  20.     for(int i = 1 ; i <= n ; i ++)
  21.         for(int j = 1 ; j <= m ; j ++)
  22.             a[i][j] = - a[i][j];
  23.     int st = 1 , dr = 0;
  24.     for(int j =1 ; j <= m ; j ++)
  25.         if(a[1][j] == 0)
  26.         {
  27.             dr ++;
  28.             x[dr] = 1 , y[dr] = j;
  29.             a[1][j] = 1;
  30.         }
  31.     while(st <= dr)
  32.     {
  33.         int i = x[st] , j = y[st];
  34.         for(int k = 0 ; k < 4 ; k ++)
  35.         {
  36.             int ii = i + dx[k] , jj = j + dy[k];
  37.             if(ii > 0 && ii <= n && jj > 0 && jj <= m && a[ii][jj] == 0)
  38.             {
  39.                 dr ++;
  40.                 x[dr] = ii, y[dr] = jj;
  41.                 a[ii][jj] = a[i][j]  + 1;
  42.             }
  43.         }
  44.         st ++;
  45.     }
  46.    
  47.     int mmin = 1000005;
  48.     for(int j = 1 ; j <= m ; j ++)
  49.         if(a[n][j] > 0)
  50.             if(a[n][j] < mmin)
  51.                 mmin = a[n][j];
  52.    
  53.     fout << mmin << "\n";
  54.    
  55.     return 0;
  56. }
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