結果
問題 | No.424 立体迷路 |
ユーザー |
|
提出日時 | 2018-06-01 20:07:48 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 2,000 ms |
コード長 | 1,525 bytes |
コンパイル時間 | 744 ms |
コンパイル使用メモリ | 74,708 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-05 07:16:04 |
合計ジャッジ時間 | 1,594 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 21 |
ソースコード
#include<iostream>#include<stack>using namespace std;int main(){int h, w, sx, sy, gx, gy;cin >> h >> w >> sx >> sy >> gx >> gy;// convert 1-origin to 0-originsx--; sy--; gx--; gy--;int G[h][w] = {};bool visit[h][w] = {};string t;for(int i = 0; i < h; i++){cin >> t;for(int j = 0; j < w; j++){G[i][j] = t[j]-'0';}}// bfsvisit[sx][sy] = true;stack<pair<int,int>> s;s.push({sx,sy});while(!s.empty()){pair<int,int> p = s.top(); s.pop();int x = p.first;int y = p.second;int z = G[x][y];visit[x][y] = true;if(x > 1 && !visit[x-2][y] && z > G[x-1][y] && z == G[x-2][y]) s.push({x-2,y});if(x < h-2 && !visit[x+2][y] && z > G[x+1][y] && z == G[x+2][y]) s.push({x+2,y});if(y > 1 && !visit[x][y-2] && z > G[x][y-1] && z == G[x][y-2]) s.push({x,y-2});if(y < w-2 && !visit[x][y+2] && z > G[x][y+1] && z == G[x][y+2]) s.push({x,y+2});if(x > 0 && !visit[x-1][y] && abs(G[x][y]-G[x-1][y]) <= 1) s.push({x-1,y});if(x < h-1 && !visit[x+1][y] && abs(G[x][y]-G[x+1][y]) <= 1) s.push({x+1,y});if(y > 0 && !visit[x][y-1] && abs(G[x][y]-G[x][y-1]) <= 1) s.push({x,y-1});if(y < w-1 && !visit[x][y+1] && abs(G[x][y]-G[x][y+1]) <= 1) s.push({x,y+1});}if(visit[gx][gy]) cout << "YES" << endl;else cout << "NO" << endl;return 0;}