Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <algorithm>
  5. using namespace std;
  6. int main()
  7. {
  8.     queue<int> q1, q2;
  9.     int n, s, i, j, m;
  10.     cin >> n >> m;
  11.     //vector<vector<pair<int, int> > > p(n, vector<pair<int, int>(n));
  12.     vector<vector<int> > v(n+2, vector<int>(m+2));
  13.     for(i = 0; i <= n+1; i++) v[i][0] = -1;
  14.     for(i = 0; i <= m+1; i++) v[0][i] = -1;
  15.     for(i = 0; i <= n+1; i++) v[i][m+1] = -1;
  16.     for(i = 0; i <= m+1; i++) v[n+1][i] = -1;
  17.  
  18.     for(i = 1; i <= n; i++)
  19.     {
  20.         for(j = 1; j <= m; j++)
  21.         {
  22.             cin >> v[i][j];
  23.             if(v[i][j] == 2)v[i][j] = -2;
  24.             if(v[i][j] == 1)v[i][j] = -1;
  25.         }
  26.     }
  27.     q1.push(1); q2.push(1);
  28.     while(!q1.empty())
  29.     {
  30.         int x = q1.front();
  31.         int y = q2.front();
  32.         q1.pop();
  33.         q2.pop();
  34.         i = y;
  35.         j = x;
  36.         while(v[i][j] == 0)
  37.         {
  38.             i--;
  39.         }
  40.         if(v[i][j] == -1)
  41.         {
  42.             i++;
  43.             v[i][j] = v[y][x]+1;
  44.             q1.push(j);
  45.             q2.push(i);
  46.         }
  47.         if(v[i][j] == -2)
  48.         {
  49.             cout << v[y][x]+1;
  50.             return 0;
  51.         }
  52.         //
  53.         i = y;
  54.         j = x;
  55.         while(v[i][j] == 0)
  56.         {
  57.             i++;
  58.         }
  59.         if(v[i][j] == -1)
  60.         {
  61.             i--;
  62.             v[i][j] = v[y][x]+1;
  63.             q1.push(j);
  64.             q2.push(i);
  65.         }
  66.         if(v[i][j] == -2)
  67.         {
  68.             cout << v[y][x]+1;
  69.             return 0;
  70.         }
  71.         //
  72.         i = y;
  73.         j = x;
  74.         while(v[i][j] == 0)
  75.         {
  76.             j++;
  77.         }
  78.         if(v[i][j] == -1)
  79.         {
  80.             j--;
  81.             v[i][j] = v[y][x]+1;
  82.             q1.push(j);
  83.             q2.push(i);
  84.         }
  85.         if(v[i][j] == -2)
  86.         {
  87.             cout << v[y][x]+1;
  88.             return 0;
  89.         }
  90.         //
  91.         i = y;
  92.         j = x;
  93.         while(v[i][j] == 0)
  94.         {
  95.             j--;
  96.         }
  97.         if(v[i][j] == -1)
  98.         {
  99.             j++;
  100.             v[i][j] = v[y][x]+1;
  101.             q1.push(j);
  102.             q2.push(i);
  103.         }
  104.         if(v[i][j] == -2)
  105.         {
  106.             cout << v[y][x]+1;
  107.             return 0;
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement