結果
| 問題 |
No.8063 幅優先探索
|
| コンテスト | |
| ユーザー |
lice6613
|
| 提出日時 | 2020-04-26 19:47:56 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 88 ms / 2,000 ms |
| コード長 | 930 bytes |
| コンパイル時間 | 6,293 ms |
| コンパイル使用メモリ | 212,024 KB |
| 最終ジャッジ日時 | 2025-01-10 02:08:47 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 9 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
int R, C, sy, sx, gy, gx;
vector<vector<char>> c;
vector<vector<int>> ans;
void bfs() {
queue<pair<int, int>> q;
q.push(make_pair(sy, sx));
ans[sy][sx] = 0;
while (!q.empty()) {
int y = q.front().first, x = q.front().second;
q.pop();
int dy[] = {1, 0, -1, 0}, dx[] = {0, 1, 0, -1};
for (int i = 0; i < 4; ++i) {
int ny = y + dy[i], nx = x + dx[i];
if (ny < 0 || R <= ny || nx < 0 || C <= nx) {
continue;
}
if (ans[ny][nx] != -1) {
continue;
}
q.push(make_pair(ny, nx));
ans[ny][nx] = ans[y][x] + 1;
}
}
}
int main() {
cin >> R >> C >> sy >> sx >> gy >> gx;
c.resize(R, vector<char>(C)), ans.resize(R, vector<int>(C, -1));
for (int i = 0; i < R; ++i) {
for (int j = 0; j < C; ++j) {
cin >> c[i][j];
}
}
--sy, --sx, --gy, --gx;
bfs();
cout << ans[gy][gx] << endl;
}
lice6613