Advertisement
Guest User

Untitled

a guest
Aug 25th, 2014
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3.  
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int arr[130][130];
  9. int main() {
  10.    for (int i = 0; i < 110; ++i)
  11.     for (int j = 0; j < 110; ++j)
  12.         arr[i][j] = -1;
  13.    int n, m, ans = 1e6;
  14.    cin >> n >> m;
  15.    for (int i = 1; i <= n; ++i)
  16.     for (int j = 1; j <= m; ++j) {
  17.         cin >> arr[i][j];
  18.         arr[i][j] *= -1;
  19.     }
  20.    queue<pair<int, pair<int, int> > > q;
  21.    pair<int, pair<int, int> > k;
  22.    k.first = 1;
  23.    k.second.first = 1;
  24.    k.second.second = 1;
  25.    q.push(k);
  26.    while(!q.empty()) {
  27.         pair<int, pair<int, int> > v = q.front();
  28.         q.pop();
  29.         if (arr[v.first + 1][v.second.first] != -1 ) {
  30.             int p = v.first + 1;
  31.             while (arr[p][v.second.first] != -1) {
  32.                 if (arr[p][v.second.first] == -2 && ans > v.second.second)
  33.                     ans = v.second.second;
  34.                 ++p;
  35.             }
  36.             if (ans != 1e6)
  37.                 break;
  38.             k.first = p - 1;
  39.             k.second.first = v.second.first;
  40.             k.second.second = v.second.second + 1;
  41.             q.push(k);
  42.         }
  43.         if (arr[v.first - 1][v.second.first] != -1) {
  44.             int p = v.first - 1;
  45.             while (arr[p][v.second.first] != -1) {
  46.                 if (arr[p][v.second.first] == -2 && ans > v.second.second)
  47.                     ans = v.second.second;
  48.                 --p;
  49.             }
  50.             if (ans != 1e6)
  51.                 break;
  52.             k.first = p + 1;
  53.             k.second.first = v.second.first;
  54.             k.second.second = v.second.second + 1;
  55.             q.push(k);
  56.         }
  57.         if (arr[v.first][v.second.first + 1] != -1) {
  58.             int p = v.second.first + 1;
  59.             while (arr[v.first][p] != -1) {
  60.                 if (arr[v.first][p] == -2 && ans > v.second.second)
  61.                     ans = v.second.second;
  62.                 ++p;
  63.             }
  64.             if (ans != 1e6)
  65.                 break;
  66.             k.first = v.first;
  67.             k.second.first = p - 1;
  68.             k.second.second = v.second.second + 1;
  69.             q.push(k);
  70.         }
  71.         if (arr[v.first][v.second.first - 1] != -1) {
  72.             int p = v.second.first - 1;
  73.             while (arr[v.first][p] != -1) {
  74.                 if (arr[v.first][p] == -2 && ans > v.second.second)
  75.                     ans = v.second.second;
  76.                 --p;
  77.             }
  78.             if (ans != 1e6)
  79.                 break;
  80.             k.first = v.first;
  81.             k.second.first = p + 1;
  82.             k.second.second = v.second.second + 1;
  83.             q.push(k);
  84.         }
  85.    }
  86.    cout << ans;
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement