結果

問題 No.1323 うしらずSwap
ユーザー ei1333333ei1333333
提出日時 2020-12-03 17:08:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,391 bytes
コンパイル時間 2,993 ms
コンパイル使用メモリ 222,644 KB
実行使用メモリ 135,936 KB
最終ジャッジ日時 2023-10-21 09:31:21
合計ジャッジ時間 20,268 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 WA -
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 WA -
testcase_16 AC 118 ms
61,800 KB
testcase_17 AC 166 ms
61,708 KB
testcase_18 AC 115 ms
61,648 KB
testcase_19 AC 161 ms
61,956 KB
testcase_20 AC 114 ms
61,648 KB
testcase_21 AC 313 ms
130,984 KB
testcase_22 AC 492 ms
131,060 KB
testcase_23 AC 313 ms
130,984 KB
testcase_24 AC 487 ms
130,988 KB
testcase_25 AC 314 ms
130,984 KB
testcase_26 AC 273 ms
133,464 KB
testcase_27 AC 263 ms
135,936 KB
testcase_28 AC 147 ms
61,648 KB
testcase_29 AC 260 ms
131,472 KB
testcase_30 AC 343 ms
131,044 KB
testcase_31 AC 262 ms
130,996 KB
testcase_32 AC 346 ms
131,044 KB
testcase_33 AC 210 ms
61,468 KB
testcase_34 AC 208 ms
61,460 KB
testcase_35 AC 213 ms
61,776 KB
testcase_36 AC 214 ms
61,984 KB
testcase_37 AC 217 ms
61,776 KB
testcase_38 AC 228 ms
61,472 KB
testcase_39 AC 228 ms
61,460 KB
testcase_40 AC 229 ms
61,776 KB
testcase_41 AC 227 ms
61,992 KB
testcase_42 AC 217 ms
61,772 KB
testcase_43 AC 109 ms
61,920 KB
testcase_44 AC 138 ms
61,756 KB
testcase_45 AC 129 ms
62,076 KB
testcase_46 AC 140 ms
61,756 KB
testcase_47 AC 115 ms
61,920 KB
testcase_48 AC 263 ms
131,060 KB
testcase_49 AC 356 ms
130,752 KB
testcase_50 AC 252 ms
130,824 KB
testcase_51 AC 216 ms
130,968 KB
testcase_52 AC 232 ms
130,824 KB
testcase_53 AC 335 ms
130,968 KB
testcase_54 AC 234 ms
130,912 KB
testcase_55 AC 355 ms
130,920 KB
testcase_56 AC 227 ms
131,400 KB
testcase_57 AC 374 ms
131,392 KB
testcase_58 AC 3 ms
4,348 KB
testcase_59 AC 5 ms
4,792 KB
testcase_60 AC 3 ms
4,348 KB
testcase_61 AC 3 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// うしっちの 想定解が バグだらけ
// @beet_aizu
#include <bits/stdc++.h>

using namespace std;


const int inf = (1 << 30) - 1;

struct Info {
  vector< vector< int > > min_cost;
  vector< vector< int > > way;
  vector< vector< pair< int, int > > > from;
};

const int vy[] = {-1, 0, 1, 0};
const int vx[] = {0, -1, 0, 1};

Info bfs(vector< string > &S, int SY, int SX, int DY = -1, int DX = -1) {
  int H = (int) S.size();
  int W = (int) S[0].size();
  queue< pair< int, int > > que;
  vector< vector< int > > min_cost(H, vector< int >(W, -1));
  auto way = min_cost;
  vector< vector< pair< int, int > > > from(H, vector< pair< int, int > >(W, make_pair(-1, -1)));
  min_cost[SY][SX] = 0;
  way[SY][SX] = 1;
  que.emplace(SY, SX);
  while(!que.empty()) {
    auto p = que.front();
    que.pop();
    for(int k = 0; k < 4; k++) {
      int ny = p.first + vy[k];
      int nx = p.second + vx[k];
      if(ny < 0 || nx < 0 || ny >= H || nx >= W || S[ny][nx] == '#') continue;
      if(p.first == SY && p.second == SX && ny == DY && nx == DX) continue;
      if(min_cost[ny][nx] != -1) {
        if(min_cost[p.first][p.second] + 1 == min_cost[ny][nx]) {
          way[ny][nx] = min(2, way[ny][nx] + way[p.first][p.second]);
        }
        continue;
      }
      min_cost[ny][nx] = min_cost[p.first][p.second] + 1;
      way[ny][nx] = way[p.first][p.second];
      from[ny][nx] = {p.first, p.second};
      que.emplace(ny, nx);
    }
  }
  return (Info) {min_cost, way, from};
}

int main() {
  int H, W, AY, AX, BY, BX;
  cin >> H >> W >> AY >> AX >> BY >> BX;
  vector< string > S(H);
  for(auto &s : S) cin >> s;
  --AY, --AX, --BY, --BX;

  Info beet = bfs(S, AY, AX);
  auto way = beet.way;
  auto min_cost = beet.min_cost;
  auto from = beet.from;
  if(way[BY][BX] == -1) {
    cout << -1 << endl;
    return 0;
  }
  if(way[BY][BX] == 2) {
    cout << min_cost[BY][BX] * 2 << endl;
    return 0;
  }
  vector< pair< int, int > > path;
  int cy = BY, cx = BX;
  while(cy != -1) {
    path.emplace_back(cy, cx);
    tie(cy, cx) = from[cy][cx];
  }
  for(auto &p : path) {
    S[p.first][p.second] = '#';
  }
  for(int i = 1; i + 1 < path.size(); i++) {
    auto &p = path[i];
    for(int j = 0; j < 4; j++) {
      int ny = p.first + vy[j];
      int nx = p.second + vx[j];
      if(ny < 0 || nx < 0 || ny >= H || nx >= W || S[ny][nx] == '#') continue;
      if(S[ny][nx] == '.') {
        cout << min_cost[BY][BX] * 2 + 2 << endl;
        return 0;
      }
    }
  }
  auto tap = bfs(S, AY, AX).min_cost;
  auto ris = bfs(S, BY, BX).min_cost;
  int ret = inf;
  for(int i = 0; i < H; i++) {
    for(int j = 0; j < W; j++) {
      int near = 0;
      for(int k = 0; k < 4; k++) {
        int ny = i + vy[k];
        int nx = j + vx[k];
        if (ny < 0 || nx < 0 || ny >= H || nx >= W || S[ny][nx] == '#') continue;
        near++;
      }
      bool luz = (AY == i && AX == j) || (BY == i && BX == j); 
      if(near >= 3 - luz) {
        if(tap[i][j] != -1) {
          ret = min(ret, min_cost[BY][BX] * 2 + tap[i][j] * 4 + 4);
        }
        if(ris[i][j] != -1) {
          ret = min(ret, min_cost[BY][BX] * 2 + ris[i][j] * 4 + 4);
        }
      }
    }
  }
  S[BY][BX] = '.';
  auto nene = bfs(S, AY, AX, BY, BX).min_cost[BY][BX];
  if(nene != -1) ret = min(ret, min_cost[BY][BX] + nene);
  if(ret >= inf) cout << -1 << endl;
  else cout << ret << endl;
}
0