Advertisement
OIQ

Escape

OIQ
Oct 23rd, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <deque>
  4. #include <utility>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10.     int n, m;
  11.     char c;
  12.  
  13.     cin >> n >> m;
  14.  
  15.     vector <vector<int>> a(n + 2, vector<int>(m + 2, -2));
  16.     deque<pair <int, int>> deq;
  17.  
  18.     int xi = -1, xj = -1, yi = -1, yj = -1;
  19.  
  20.     for (int i = 1; i < n + 1; i++)
  21.         for (int j = 1; j < m + 1; j++) {
  22.             cin >> c;
  23.             if (c == 's') {
  24.                 a[i][j] = 0;
  25.                 deq.push_back(pair <int, int>(i, j));
  26.                 xi = i;
  27.                 xj = j;
  28.             }
  29.             else if (c == 'f') {
  30.                 yi = i;
  31.                 yj = j;
  32.                 a[i][j] = -1;
  33.             }
  34.             else if (c == '.')
  35.                 a[i][j] = -1;
  36.         }
  37.  
  38.     while (!deq.empty()) {
  39.         pair <int, int> p (deq.front());
  40.         deq.pop_front();
  41.  
  42.         if (a[p.first - 1][p.second] == -1) {
  43.             a[p.first - 1][p.second] = a[p.first][p.second] + 1;
  44.             deq.push_back(pair<int, int>(p.first - 1, p.second));
  45.         }
  46.         if (a[p.first][p.second + 1] == -1) {
  47.             a[p.first][p.second + 1] = a[p.first][p.second] + 1;
  48.             deq.push_back(pair<int, int>(p.first, p.second + 1));
  49.         }
  50.         if (a[p.first + 1][p.second] == -1) {
  51.             a[p.first + 1][p.second] = a[p.first][p.second] + 1;
  52.             deq.push_back(pair<int, int>(p.first + 1, p.second));
  53.         }
  54.         if (a[p.first][p.second - 1] == -1) {
  55.             a[p.first][p.second - 1] = a[p.first][p.second] + 1;
  56.             deq.push_back(pair<int, int>(p.first, p.second - 1));
  57.         }
  58.  
  59.  
  60.     }
  61.  
  62.  
  63.     if (a[yi][yj] == -1) {
  64.         cout << -1;
  65.     }
  66.     else {
  67.         bool flag = false;
  68.         if (xi == yi) {
  69.            
  70.             for (int i = 1; i < n + 1; i++)
  71.                 for (int j = 1; j < m + 1; j++)
  72.                     if (a[i][j] != -1 && (i != yi)) {
  73.                         flag = true;
  74.                         break;
  75.                     }
  76.         }
  77.         else if (xj == yj) {
  78.                 for (int i = 1; i < n + 1; i++)
  79.                     for (int j = 1; j < m + 1; j++)
  80.                         if (a[i][j] != -1 && (j != yj)) {
  81.                             flag = true;
  82.                             break;
  83.                         }
  84.         }
  85.  
  86.         if (flag == false)
  87.             cout << (abs(xi - yi) + abs(xj - yj)) % 2;
  88.         else {
  89.             if (xi == yi)
  90.                 cout << abs(xj - yj);
  91.             else cout << abs(xi - yi);
  92.         }
  93.  
  94.     }
  95.  
  96.  
  97.  
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement