結果
問題 |
No.2096 Rage With Our Friends
|
ユーザー |
![]() |
提出日時 | 2022-09-14 23:41:06 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 292 ms / 5,000 ms |
コード長 | 3,616 bytes |
コンパイル時間 | 1,075 ms |
コンパイル使用メモリ | 88,572 KB |
最終ジャッジ日時 | 2025-02-07 05:28:22 |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
#include <iostream> #include <vector> #include <string> #include <deque> #include <cassert> using namespace std; struct nodes{ int x; int y; }; int main(){ //1. 入力を読む 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]; } assert(S[sx][sy] == '.'); assert(S[gx][gy] == '.'); //2. 全ての座標に対して,その真下にある座標で一番上にあるものがどこかのx座標を求める vector<int> calc_highest_x(W, 0); vector<vector<int>> highest_x(H, vector<int>(W, 0)); for (int i = 0; i < H; i++){ for (int j = 0; j < W; j++){ if (S[i][j] == '.') calc_highest_x[j] = i; highest_x[i][j] = calc_highest_x[j]; } } int INF = 1 << 24; //3. 01-BFSを行う vector<vector<bool>> isvisited(H, vector<bool>(W, false)); vector<vector<int>> dist(H, vector<int>(W, INF)); deque<nodes> dq; dq.push_front(nodes{sx, sy}); dist[sx][sy] = 0; while(dq.size()){ nodes p = dq.front(); dq.pop_front(); if (p.x == gx && p.y == gy){ cout << dist[gx][gy] << endl; return 0; } if (isvisited[p.x][p.y]){ continue; } isvisited[p.x][p.y] = true; //1回の移動で行ける範囲を探索 if (p.y - 1 >= 0){ int next_x = min(H - 1, p.x + 1); next_x = highest_x[next_x][p.y - 1]; dq.push_front(nodes{next_x, p.y - 1}); dist[next_x][p.y - 1] = min(dist[next_x][p.y - 1], dist[p.x][p.y]); } if (p.y + 1 < W){ int next_x = min(H - 1, p.x + 1); next_x = highest_x[next_x][p.y + 1]; dq.push_front(nodes{next_x, p.y + 1}); dist[next_x][p.y + 1] = min(dist[next_x][p.y + 1], dist[p.x][p.y]); } //2回の移動で行ける範囲を探索 if (p.y - 2 >= 0){ int next_x = min(H - 1, p.x + 1); next_x = highest_x[next_x][p.y - 2]; dq.push_back(nodes{next_x, p.y - 2}); dist[next_x][p.y - 2] = min(dist[next_x][p.y - 2], dist[p.x][p.y] + 1); } if (p.y - 2 >= 0){ int diffs_x = -1 * (highest_x[p.x][p.y - 1] - p.x); int next_x = highest_x[p.x][p.y - 1] + 1 + diffs_x / 2; next_x = highest_x[min(H - 1, next_x)][p.y - 2]; dq.push_front(nodes{next_x, p.y - 2}); dist[next_x][p.y - 2] = min(dist[next_x][p.y - 2], dist[p.x][p.y]); } if (p.y + 2 < W){ int next_x = min(H - 1, p.x + 1); next_x = highest_x[next_x][p.y + 2]; dq.push_back(nodes{next_x, p.y + 2}); dist[next_x][p.y + 2] = min(dist[next_x][p.y + 2], dist[p.x][p.y] + 1); } if (p.y + 2 < W){ int diffs_x = -1 * (highest_x[p.x][p.y + 1] - p.x); int next_x = highest_x[p.x][p.y + 1] + 1 + diffs_x / 2; next_x = highest_x[min(H - 1, next_x)][p.y + 2]; dq.push_front(nodes{next_x, p.y + 2}); dist[next_x][p.y + 2] = min(dist[next_x][p.y + 2], dist[p.x][p.y]); } if (p.y >= 0 && p.y < W){ int next_x = min(H - 1, p.x + 1); next_x = highest_x[next_x][p.y]; dq.push_back(nodes{next_x, p.y}); dist[next_x][p.y] = min(dist[next_x][p.y], dist[p.x][p.y] + 1); } } cout << -1 << endl; return 0; }