結果
問題 |
No.8063 幅優先探索
|
ユーザー |
|
提出日時 | 2020-04-01 22:01:14 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,583 bytes |
コンパイル時間 | 1,791 ms |
コンパイル使用メモリ | 182,668 KB |
実行使用メモリ | 73,600 KB |
最終ジャッジ日時 | 2024-06-27 10:52:08 |
合計ジャッジ時間 | 3,587 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 5 WA * 4 |
ソースコード
#include <bits/stdc++.h> using namespace std; int main() { int h, w; cin >> h >> w; vector<vector<int>> g(h+2, vector<int>(w+2, 1)); vector<vector<int>> ans(h+2, vector<int>(w+2, 1001001001)); map<pair<int, int>, bool> seen; pair<int, int> start, goal; cin >> start.first >> start.second; cin >> goal.first >> goal.second; for(int i=1;i<=h;i++) for(int j=1;j<=w;j++) { char c; cin >> c; if(c == '.') g[i][j] = 0; } queue<pair<int, int>> que; que.emplace(start); ans[start.first][start.second] = 0; while(!que.empty()) { pair<int, int> p = que.front();que.pop(); if(seen[p]) continue; seen[p] = true; int now = ans[p.first][p.second]; if(!g[p.first][p.second+1] && ans[p.first][p.second+1] > now + 1) { ans[p.first][p.second+1] = now + 1; que.emplace(make_pair(p.first, p.second+1)); } if(!g[p.first][p.second-1] && ans[p.first][p.second-1] > now + 1) { ans[p.first][p.second-1] = now + 1; que.emplace(make_pair(p.first, p.second-1)); } if(!g[p.first-1][p.second] && ans[p.first-1][p.second] > now + 1) { ans[p.first-1][p.second] = now + 1; que.emplace(make_pair(p.first-1, p.second)); } if(!g[p.first+1][p.second] && ans[p.first+1][p.second] > now + 1) { ans[p.first+1][p.second] = now + 1; que.emplace(make_pair(p.first+1, p.second)); } } cout << ans[goal.first][goal.second] << endl; return 0; }