結果
問題 | No.1323 うしらずSwap |
ユーザー | ei1333333 |
提出日時 | 2020-12-07 23:45:17 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 167 ms / 3,000 ms |
コード長 | 4,236 bytes |
コンパイル時間 | 3,119 ms |
コンパイル使用メモリ | 216,936 KB |
実行使用メモリ | 38,040 KB |
最終ジャッジ日時 | 2024-11-15 09:03:42 |
合計ジャッジ時間 | 10,878 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 3 ms
5,248 KB |
testcase_08 | AC | 2 ms
5,248 KB |
testcase_09 | AC | 2 ms
5,248 KB |
testcase_10 | AC | 2 ms
5,248 KB |
testcase_11 | AC | 2 ms
5,248 KB |
testcase_12 | AC | 2 ms
5,248 KB |
testcase_13 | AC | 2 ms
5,248 KB |
testcase_14 | AC | 2 ms
5,248 KB |
testcase_15 | AC | 2 ms
5,248 KB |
testcase_16 | AC | 74 ms
26,880 KB |
testcase_17 | AC | 115 ms
27,136 KB |
testcase_18 | AC | 75 ms
27,008 KB |
testcase_19 | AC | 119 ms
27,136 KB |
testcase_20 | AC | 74 ms
27,008 KB |
testcase_21 | AC | 109 ms
33,664 KB |
testcase_22 | AC | 164 ms
33,792 KB |
testcase_23 | AC | 110 ms
33,664 KB |
testcase_24 | AC | 167 ms
33,664 KB |
testcase_25 | AC | 109 ms
33,792 KB |
testcase_26 | AC | 106 ms
35,620 KB |
testcase_27 | AC | 113 ms
38,040 KB |
testcase_28 | AC | 98 ms
27,008 KB |
testcase_29 | AC | 101 ms
33,664 KB |
testcase_30 | AC | 143 ms
33,664 KB |
testcase_31 | AC | 101 ms
33,664 KB |
testcase_32 | AC | 152 ms
33,792 KB |
testcase_33 | AC | 161 ms
26,752 KB |
testcase_34 | AC | 162 ms
26,880 KB |
testcase_35 | AC | 163 ms
26,880 KB |
testcase_36 | AC | 158 ms
27,008 KB |
testcase_37 | AC | 155 ms
27,008 KB |
testcase_38 | AC | 161 ms
26,752 KB |
testcase_39 | AC | 161 ms
27,008 KB |
testcase_40 | AC | 163 ms
26,880 KB |
testcase_41 | AC | 163 ms
27,008 KB |
testcase_42 | AC | 152 ms
26,880 KB |
testcase_43 | AC | 70 ms
27,008 KB |
testcase_44 | AC | 95 ms
26,880 KB |
testcase_45 | AC | 87 ms
27,008 KB |
testcase_46 | AC | 96 ms
27,008 KB |
testcase_47 | AC | 76 ms
27,008 KB |
testcase_48 | AC | 87 ms
33,792 KB |
testcase_49 | AC | 122 ms
33,536 KB |
testcase_50 | AC | 84 ms
33,536 KB |
testcase_51 | AC | 66 ms
33,664 KB |
testcase_52 | AC | 72 ms
33,664 KB |
testcase_53 | AC | 118 ms
33,664 KB |
testcase_54 | AC | 73 ms
33,664 KB |
testcase_55 | AC | 123 ms
33,664 KB |
testcase_56 | AC | 67 ms
33,664 KB |
testcase_57 | AC | 131 ms
33,792 KB |
testcase_58 | AC | 2 ms
5,248 KB |
testcase_59 | AC | 3 ms
5,248 KB |
testcase_60 | AC | 2 ms
5,248 KB |
testcase_61 | AC | 2 ms
5,248 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using pi = pair< int, int >; constexpr int inf = 1 << 30; constexpr int vy[] = {-1, 1, 0, 0}; constexpr int vx[] = {0, 0, -1, 1}; template< typename T, typename E > bool chmin(T &a, E b) { if(a > b) { a = b; return true; } else { return false; } } int main() { int H, W, Ya, Xa, Yb, Xb; cin >> H >> W >> Ya >> Xa >> Yb >> Xb; --Ya, --Xa, --Yb, --Xb; vector< string > S(H); for(auto &s : S) cin >> s; vector< vector< int > > min_cost(H, vector< int >(W, inf)); vector< vector< int > > ways(H, vector< int >(W, 0)); vector< vector< int > > from(H, vector< int >(W, -1)); { queue< pi > que; min_cost[Ya][Xa] = 0; ways[Ya][Xa] = 1; que.emplace(Ya, Xa); while(!que.empty()) { auto[y, x] = que.front(); que.pop(); for(int k = 0; k < 4; k++) { int ny = y + vy[k]; int nx = x + vx[k]; if(S[ny][nx] == '#') continue; if(chmin(min_cost[ny][nx], min_cost[y][x] + 1)) { from[ny][nx] = k; ways[ny][nx] = 0; que.emplace(ny, nx); } if(min_cost[y][x] + 1 == min_cost[ny][nx]) { ways[ny][nx] += ways[y][x]; chmin(ways[ny][nx], 2); } } } } if(ways[Yb][Xb] == 0) { cout << -1 << "\n"; return 0; } if(ways[Yb][Xb] >= 2) { cout << min_cost[Yb][Xb] * 2 << "\n"; return 0; } vector< pair< int, int > > path; int cy = Yb, cx = Xb; path.emplace_back(cy, cx); while(cy != Ya || cx != Xa) { int py = cy + vy[from[cy][cx] ^ 1]; int px = cx + vx[from[cy][cx] ^ 1]; cy = py; cx = px; path.emplace_back(cy, cx); } assert(min_cost[Yb][Xb] + 1 == path.size()); for(auto &p : path) S[p.first][p.second] = '*'; for(int i = 1; i + 1 < path.size(); i++) { for(int j = 0; j < 4; j++) { int ny = path[i].first + vy[j]; int nx = path[i].second + vx[j]; if(S[ny][nx] == '.') { cout << min_cost[Yb][Xb] * 2 + 2 << "\n"; return 0; } } } int ret = inf; int pre = min_cost[Yb][Xb]; { { queue< pi > que; min_cost[Yb][Xb] = 0; que.emplace(Yb, Xb); while(!que.empty()) { auto[y, x] = que.front(); que.pop(); for(int k = 0; k < 4; k++) { int ny = y + vy[k]; int nx = x + vx[k]; if(S[ny][nx] == '#') continue; if(chmin(min_cost[ny][nx], min_cost[y][x] + 1)) { que.emplace(ny, nx); } } } } for(int i = 1; i + 1 < H; i++) { for(int j = 1; j + 1 < W; j++) { if(S[i][j] == '.') { int deg = 0; for(int k = 0; k < 4; k++) { int ny = i + vy[k]; int nx = j + vx[k]; if(S[ny][nx] != '#') { ++deg; } } if(deg >= 3 && min_cost[i][j] != inf) { chmin(ret, min_cost[i][j] * 4 + 4 + pre * 2); } } } } { int deg = 0; for(int k = 0; k < 4; k++) { int ny = Ya + vy[k]; int nx = Xa + vx[k]; if(S[ny][nx] == '.') { ++deg; } } if(deg >= 2) { chmin(ret, 4 + pre * 2); } } { int deg = 0; for(int k = 0; k < 4; k++) { int ny = Yb + vy[k]; int nx = Xb + vx[k]; if(S[ny][nx] == '.') { ++deg; } } if(deg >= 2) { chmin(ret, 4 + pre * 2); } } } { vector< vector< int > > min_cost2(H, vector< int >(W, inf)); queue< pi > que; min_cost2[Ya][Xa] = 0; que.emplace(Ya, Xa); S[Ya][Xa] = '.'; S[Yb][Xb] = '.'; while(!que.empty()) { auto[y, x] = que.front(); que.pop(); for(int k = 0; k < 4; k++) { int ny = y + vy[k]; int nx = x + vx[k]; if(S[ny][nx] == '#' || S[ny][nx] == '*') continue; if(y == Ya && x == Xa && ny == Yb && nx == Xb) continue; if(chmin(min_cost2[ny][nx], min_cost2[y][x] + 1)) { que.emplace(ny, nx); } } } if(min_cost2[Yb][Xb] != inf) { chmin(ret, pre + min_cost2[Yb][Xb]); } } if(ret >= inf) ret = -1; cout << ret << "\n"; }