Advertisement
Guest User

Untitled

a guest
Apr 19th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <deque>
  4.  
  5. using namespace std;
  6.  
  7. struct point {
  8.     int i, j;
  9.     int way;
  10.  
  11.     explicit point(int a = 0, int b = 0, int w = 0) : i(a), j(b), way(w) {
  12.     }
  13. };
  14.  
  15. int main() {
  16.     int h, w;
  17.     cin >> h >> w;
  18.     vector<vector<vector<int>>> field(8, vector<vector<int>>(h));
  19.     for (int k = 0; k < 8; ++k) {
  20.         for (int i = 0; i < h; ++i) {
  21.             field[k][i].resize(w);
  22.             for (int j = 0; j < w; ++j) {
  23.                 char t;
  24.                 cin >> t;
  25.                 if (t == '.') {
  26.                     field[k][i][j] = 1000000;
  27.                 }
  28.                 if (t == 'X') {
  29.                     field[k][i][j] = -1;
  30.                 }
  31.             }
  32.         }
  33.     }
  34.     int sx, sy;
  35.     cin >> sx >> sy;
  36.     int tx, ty;
  37.     cin >> tx >> ty;
  38.     int way = 0;
  39.     deque<point> que;
  40.     --sx;
  41.     sy = h - sy;
  42.     --tx;
  43.     ty = h - ty;
  44.     for (int i = 0; i < 8; ++i) {
  45.         field[i][sy][sx] = 0;
  46.     }
  47.     que.push_back(point(sy, sx, 0));
  48.     vector<int> di = {-1, 0, 1, 0, -1, 1, 1, -1};
  49.     vector<int> dj = {0, 1, 0, -1, 1, 1, -1, -1};
  50.     while (!que.empty()) {
  51.         point cur(que.front().i, que.front().j, que.front().way);
  52.         que.pop_front();
  53.         for (int k = 1; k <= 8; ++k) {
  54.             point neib(cur.i + di[k - 1], cur.j + dj[k - 1]);
  55.             if (neib.i < 0 || neib.i >= h || neib.j < 0 || neib.j >= w) {
  56.                 continue;
  57.             }
  58.             if (field[k - 1][neib.i][neib.j] == -1) {
  59.                 continue;
  60.             }
  61.             if (cur.way == k) {
  62.                 if (field[k - 1][neib.i][neib.j] > field[k - 1][cur.i][cur.j]) {
  63.                     field[k - 1][neib.i][neib.j] = field[k - 1][cur.i][cur.j];
  64.                     que.push_front(point(neib.i, neib.j, k));
  65.                 }
  66.             } else {
  67.                 if (field[k - 1][neib.i][neib.j] > field[k - 1][cur.i][cur.j] + 1) {
  68.                     field[k - 1][neib.i][neib.j] = field[k - 1][cur.i][cur.j] + 1;
  69.                     que.push_back(point(neib.i, neib.j, k));
  70.                 }
  71.             }
  72.         }
  73.     }
  74.     int ans = field[0][ty][tx];
  75.     for (int i = 1; i < 8; ++i) {
  76.         ans = min(ans, field[i][ty][tx]);
  77.     }
  78.     if (ans == -1 || ans == 1000000000) {
  79.         cout << -1 << endl;
  80.         return 0;
  81.     }
  82.     cout << ans << endl;
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement