結果
問題 | No.1323 うしらずSwap |
ユーザー | milanis48663220 |
提出日時 | 2020-12-20 15:27:34 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,463 ms / 3,000 ms |
コード長 | 5,946 bytes |
コンパイル時間 | 1,358 ms |
コンパイル使用メモリ | 108,904 KB |
実行使用メモリ | 276,636 KB |
最終ジャッジ日時 | 2023-08-09 13:59:29 |
合計ジャッジ時間 | 29,293 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge14 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 35 ms
93,488 KB |
testcase_01 | AC | 35 ms
93,500 KB |
testcase_02 | AC | 36 ms
93,704 KB |
testcase_03 | AC | 34 ms
93,480 KB |
testcase_04 | AC | 34 ms
91,448 KB |
testcase_05 | AC | 34 ms
93,528 KB |
testcase_06 | AC | 35 ms
93,492 KB |
testcase_07 | AC | 34 ms
93,716 KB |
testcase_08 | AC | 33 ms
93,460 KB |
testcase_09 | AC | 35 ms
93,476 KB |
testcase_10 | AC | 34 ms
93,568 KB |
testcase_11 | AC | 35 ms
93,652 KB |
testcase_12 | AC | 34 ms
93,488 KB |
testcase_13 | AC | 34 ms
93,512 KB |
testcase_14 | AC | 34 ms
93,568 KB |
testcase_15 | AC | 34 ms
93,520 KB |
testcase_16 | AC | 295 ms
166,648 KB |
testcase_17 | AC | 528 ms
166,504 KB |
testcase_18 | AC | 299 ms
166,580 KB |
testcase_19 | AC | 575 ms
166,368 KB |
testcase_20 | AC | 298 ms
166,452 KB |
testcase_21 | AC | 334 ms
165,200 KB |
testcase_22 | AC | 583 ms
166,576 KB |
testcase_23 | AC | 333 ms
165,220 KB |
testcase_24 | AC | 630 ms
166,444 KB |
testcase_25 | AC | 333 ms
166,564 KB |
testcase_26 | AC | 363 ms
162,468 KB |
testcase_27 | AC | 383 ms
159,832 KB |
testcase_28 | AC | 541 ms
166,456 KB |
testcase_29 | AC | 320 ms
166,572 KB |
testcase_30 | AC | 413 ms
165,296 KB |
testcase_31 | AC | 314 ms
165,164 KB |
testcase_32 | AC | 462 ms
166,500 KB |
testcase_33 | AC | 1,150 ms
223,320 KB |
testcase_34 | AC | 1,237 ms
242,552 KB |
testcase_35 | AC | 1,347 ms
252,116 KB |
testcase_36 | AC | 1,244 ms
257,768 KB |
testcase_37 | AC | 1,241 ms
260,988 KB |
testcase_38 | AC | 1,407 ms
273,924 KB |
testcase_39 | AC | 1,463 ms
275,280 KB |
testcase_40 | AC | 1,454 ms
275,976 KB |
testcase_41 | AC | 1,463 ms
276,636 KB |
testcase_42 | AC | 1,205 ms
275,256 KB |
testcase_43 | AC | 248 ms
133,564 KB |
testcase_44 | AC | 360 ms
139,016 KB |
testcase_45 | AC | 409 ms
148,368 KB |
testcase_46 | AC | 400 ms
138,836 KB |
testcase_47 | AC | 300 ms
139,120 KB |
testcase_48 | AC | 245 ms
132,164 KB |
testcase_49 | AC | 349 ms
139,048 KB |
testcase_50 | AC | 231 ms
128,484 KB |
testcase_51 | AC | 83 ms
112,120 KB |
testcase_52 | AC | 150 ms
121,408 KB |
testcase_53 | AC | 372 ms
137,752 KB |
testcase_54 | AC | 156 ms
121,440 KB |
testcase_55 | AC | 346 ms
138,968 KB |
testcase_56 | AC | 112 ms
116,984 KB |
testcase_57 | AC | 495 ms
146,944 KB |
testcase_58 | AC | 36 ms
93,704 KB |
testcase_59 | AC | 36 ms
94,220 KB |
testcase_60 | AC | 36 ms
93,716 KB |
testcase_61 | AC | 35 ms
93,504 KB |
ソースコード
#include <iostream> #include <algorithm> #include <iomanip> #include <vector> #include <queue> #include <set> #include <map> #include <cassert> #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; const int INF = 1e+9; typedef pair<int, int> P; struct edge{ int to; int cost; }; void dijkstra(int s, int num_v, vector<edge> G[], int d[]){ priority_queue<P, vector<P>, greater<P>> que; fill(d, d+num_v, INF); d[s] = 0; que.push(P(0, s)); while(!que.empty()){ P p = que.top();que.pop(); int v = p.second; if(d[v] < p.first) continue; for(int i = 0; i < G[v].size(); i++){ edge e = G[v][i]; if(d[v] + e.cost < d[e.to]){ d[e.to] = d[v] + e.cost; que.push(P(d[e.to], e.to)); } } } } int h, w, ra, ca, rb, cb; char s[1500][1500]; int dh[4] = {0, 0, -1, 1}; int dw[4] = {-1, 1, 0, 0}; bool is_in_field(int i, int j){ return i >= 0 && i < h && j >= 0 && j < w; } int to_idx(int i, int j){ return i*w+j; } int to_i(int idx){ return idx/w; } int to_j(int idx){ return idx%w; } void input(){ cin >> h >> w >> ra >> ca >> rb >> cb; ra--; ca--; rb--; cb--; for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ cin >> s[i][j]; } } } vector<edge> G[1333*1333]; int dist[1333*1333]; vector<P> get_shortest_path(){ int cur_h = rb, cur_w = cb; vector<P> ans; ans.push_back(P(cur_h, cur_w)); while(cur_h != ra || cur_w != ca){ for(int i = 0; i < 4; i++){ int h_to = cur_h+dh[i]; int w_to = cur_w+dw[i]; if(is_in_field(h_to, w_to) && dist[to_idx(h_to, w_to)] == dist[to_idx(cur_h, cur_w)]-1){ cur_h = h_to; cur_w = w_to; break; } } ans.push_back(P(cur_h, cur_w)); } return ans; } vector<edge> G_rem[1333*1333]; bool used_in_path[1333][1333]; void make_g_rem(){ for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ if(s[i][j] == '#') continue; for(int k = 0; k < 4; k++){ int i_to = i+dh[k], j_to = j+dw[k]; if(is_in_field(i_to, j_to) && s[i_to][j_to] == '.'){ if(used_in_path[i][j] && used_in_path[i_to][j_to]) continue; G_rem[to_idx(i, j)].push_back((edge){to_idx(i_to, j_to), 1}); } } } } } // 三箇所と繋がっているところまでの距離 int calc_dist_three(int from){ vector<int> dist(h*w, INF); queue<int> que; dist[from] = 0; que.push(from); while(!que.empty()){ int v = que.front(); que.pop(); int cnt = 0; int i = to_i(v), j = to_j(v); assert(s[i][j] == '.'); for(int k = 0; k < 4; k++){ int i_to = i+dh[k], j_to = j+dw[k]; if(is_in_field(i_to, j_to) && s[i_to][j_to] == '.'){ cnt++; if(dist[to_idx(i_to, j_to)] == INF){ dist[to_idx(i_to, j_to)] = dist[v]+1; que.push(to_idx(i_to, j_to)); } } } if(cnt >= 3){ return dist[v]; } } return INF; } int dist_rem[1333*1333]; void solve(){ for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ if(s[i][j] == '#') continue; for(int k = 0; k < 4; k++){ int i_to = i+dh[k], j_to = j+dw[k]; if(is_in_field(i_to, j_to) && s[i_to][j_to] == '.'){ G[to_idx(i, j)].push_back((edge){to_idx(i_to, j_to), 1}); } } } } dijkstra(to_idx(ra, ca), h*w, G, dist); if(dist[to_idx(rb, cb)] == INF){ cout << -1 << endl; return; } vector<P> path = get_shortest_path(); for(auto p : path){ used_in_path[p.first][p.second] = true; // cout << p.first << ',' << p.second << endl; } make_g_rem(); // dijkstraっぽい感じ int min_diff = INF; priority_queue<P, vector<P>, greater<P>> que; fill(dist_rem, dist_rem+h*w, INF); for(auto p : path){ int idx = to_idx(p.first, p.second); dist_rem[idx] = dist[idx]; if(p.first == rb && p.second == cb) continue; que.push(P(dist[idx], idx)); } while(!que.empty()){ P p = que.top();que.pop(); int v = p.second; if(dist_rem[v] < p.first) continue; for(int i = 0; i < G_rem[v].size(); i++){ edge e = G_rem[v][i]; if(dist_rem[v] + e.cost < dist_rem[e.to] && e.to != to_idx(rb, cb)){ dist_rem[e.to] = dist_rem[v] + e.cost; que.push(P(dist_rem[e.to], e.to)); } if(used_in_path[to_i(e.to)][to_j(e.to)] && e.to != to_idx(ra, ca)){ chmin(min_diff, dist_rem[v] + e.cost - dist_rem[e.to]); // cout << to_i(v) << ',' << to_j(v) << ' ' << to_i(e.to) << ',' << to_j(e.to) << endl; } } } assert(min_diff >= 0); int tmp = calc_dist_three(to_idx(ra, ca)); if(tmp != INF) chmin(min_diff, 4*(tmp+1)); tmp = calc_dist_three(to_idx(rb, cb)); if(tmp != INF) chmin(min_diff, 4*(tmp+1)); if(min_diff == INF){ cout << -1 << endl; }else{ cout << 2*dist[to_idx(rb, cb)]+min_diff << endl; } } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; input(); solve(); }