結果

問題 No.3063 幅優先探索
ユーザー lice6613lice6613
提出日時 2020-04-26 19:47:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 68 ms / 2,000 ms
コード長 930 bytes
コンパイル時間 2,748 ms
コンパイル使用メモリ 217,964 KB
実行使用メモリ 8,320 KB
最終ジャッジ日時 2023-08-11 06:55:23
合計ジャッジ時間 3,428 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 68 ms
8,292 KB
testcase_07 AC 68 ms
8,320 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0