Advertisement
Guest User

4b-4.cpp

a guest
Apr 24th, 2014
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4.  
  5. int peta[150][150];
  6. int n,m;
  7. int mn = 99999;
  8.  
  9. int main()
  10. {
  11.     cin >> n >> m;
  12.     for (int i=0; i<n; i++){
  13.         for (int j=0; j<m; j++){
  14.             cin >> peta[i][j];
  15.         }
  16.     }
  17.     int a,b;
  18.     cin >> a >> b;
  19.     a--; b--;
  20.  
  21.     queue<int> bfsr;
  22.     queue<int> bfsc;
  23.     queue<int> bfsstepcount;
  24.     bfsr.push(a);
  25.     bfsc.push(b);
  26.     bfsstepcount.push(1);
  27.     while(!bfsr.empty()){
  28.         int r = bfsr.front();
  29.         int c = bfsc.front();
  30.         int sc = bfsstepcount.front();
  31.         bfsr.pop();
  32.         bfsc.pop();
  33.         bfsstepcount.pop();
  34.  
  35.         if ((r <= 0) || (r >= n-1) || (c <= 0) || (c >= m-1)){
  36.             mn = sc;
  37.             break;
  38.         }
  39.  
  40.             if (peta[r-1][c] == 0){
  41.                 bfsr.push(r-1);
  42.                 bfsc.push(c);
  43.                 bfsstepcount.push(sc+1);
  44.             }
  45.  
  46.             if (peta[r+1][c] == 0){
  47.                 bfsr.push(r+1);
  48.                 bfsc.push(c);
  49.                 bfsstepcount.push(sc+1);
  50.             }
  51.  
  52.             if (peta[r][c-1] == 0){
  53.                 bfsr.push(r);
  54.                 bfsc.push(c-1);
  55.                 bfsstepcount.push(sc+1);
  56.             }
  57.  
  58.             if (peta[r][c+1] == 0){
  59.                 bfsr.push(r);
  60.                 bfsc.push(c+1);
  61.                 bfsstepcount.push(sc+1);
  62.             }
  63.     }
  64.  
  65.     cout << mn << endl;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement