結果

問題 No.2096 Rage With Our Friends
ユーザー SSRSSSRS
提出日時 2022-10-07 22:10:04
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 636 ms / 5,000 ms
コード長 3,225 bytes
コンパイル時間 2,087 ms
コンパイル使用メモリ 186,476 KB
実行使用メモリ 178,764 KB
最終ジャッジ日時 2024-06-12 18:46:28
合計ジャッジ時間 5,707 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000;
int main(){
  int H, W;
  cin >> H >> W;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  vector<string> S(H);
  for (int i = 0; i < H; i++){
    cin >> S[i];
  }
  vector<int> mn(W, INF);
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      if (S[i][j] == '.'){
        mn[j] = min(mn[j], i);
      }
    }
  }
  vector<vector<int>> mx(H, vector<int>(W, -INF));
  for (int i = 0; i < H; i++){
    for (int j = 0; j < W; j++){
      if (i > 0){
        mx[i][j] = mx[i - 1][j];
      }
      if (S[i][j] == '.'){
        mx[i][j] = i;
      }
    }
  }
  vector<vector<vector<int>>> d(2, vector<vector<int>>(H, vector<int>(W, INF)));
  deque<tuple<int, int, int, int>> dq;
  dq.push_back(make_tuple(0, 0, sx, sy));
  while (!dq.empty()){
    int c = get<0>(dq.front());
    int t = get<1>(dq.front());
    int x = get<2>(dq.front());
    int y = get<3>(dq.front());
    dq.pop_front();
    if (d[t][x][y] == INF){
      d[t][x][y] = c;
      if (t == 0){
        if (y > 0){
          int m = mx[min(x + 1, H - 1)][y - 1];
          if (m != -INF){
            if (d[1][m][y - 1] == INF){
              dq.push_front(make_tuple(c, 1, m, y - 1));
            }
            int x2 = min(x + 1, H - 1);
            if (y > 1){
              if (d[1][x2][y - 2] == INF){
                dq.push_back(make_tuple(c + 1, 1, x2, y - 2));
              }
            }
            if (d[1][x2][y] == INF){
              dq.push_back(make_tuple(c + 1, 1, x2, y));
            }
            int x3 = min(m + 1 + max(x - m, 0) / 2, H - 1);
            if (y > 1){
              if (d[1][x3][y - 2] == INF){
                dq.push_front(make_tuple(c, 1, x3, y - 2));
              }
            }
            if (d[1][x3][y] == INF){
              dq.push_front(make_tuple(c, 1, x3, y));
            }
          }
        }
        if (y < W - 1){
          int m = mx[min(x + 1, H - 1)][y + 1];
          if (m != -INF){
            if (d[1][m][y + 1] == INF){
              dq.push_front(make_tuple(c, 1, m, y + 1));
            }
            int x2 = min(x + 1, H - 1);
            if (y < W - 2){
              if (d[1][x2][y + 2] == INF){
                dq.push_back(make_tuple(c + 1, 1, x2, y + 2));
              }
            }
            if (d[1][x2][y] == INF){
              dq.push_back(make_tuple(c + 1, 1, x2, y));
            }
            int x3 = min(m + 1 + max(x - m, 0) / 2, H - 1);
            if (y < W - 2){
              if (d[1][x3][y + 2] == INF){
                dq.push_front(make_tuple(c, 1, x3, y + 2));
              }
            }
            if (d[1][x3][y] == INF){
              dq.push_front(make_tuple(c, 1, x3, y));
            }
          }
        }
      }
      if (t == 1){
        if (S[x][y] == '.'){
          if (d[0][x][y] == INF){
            dq.push_front(make_tuple(c, 0, x, y));
          }
        }
        if (x > 0){
          if (d[1][x - 1][y] == INF){
            dq.push_front(make_tuple(c, 1, x - 1, y));
          }
        }
      }
    }
  }
  if (d[0][gx][gy] == INF){
    cout << -1 << endl;
  } else {
    cout << d[0][gx][gy] << endl;
  }
}
0