結果

問題 No.424 立体迷路
コンテスト
ユーザー rsk0315
提出日時 2019-02-01 20:29:30
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,365 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 589 ms
コンパイル使用メモリ 97,572 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-06-07 06:33:30
合計ジャッジ時間 1,945 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <cstdio>
#include <cstdint>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <utility>
#include <tuple>

static constexpr size_t di[] = {size_t(-1), 0, 1, 0};
static constexpr size_t dj[] = {0, size_t(-1), 0, 1};

int main() {
  size_t h, w;
  scanf("%zu %zu", &h, &w);

  size_t sy, sx, gy, gx;
  scanf("%zu %zu %zu %zu", &sy, &sx, &gy, &gx);
  --sy;
  --sx;
  --gy;
  --gx;

  std::vector<std::string> b(h);
  for (size_t i = 0; i < h; ++i) {
    char buf[64];
    scanf("%s", buf);
    b[i] = buf;
  }

  std::queue<std::pair<size_t, size_t>> q;
  std::vector<std::vector<bool>> vis(h, std::vector<bool>(w));
  q.emplace(sy, sx);
  while (!q.empty()) {
    size_t i, j;
    std::tie(i, j) = q.front();
    q.pop();
    if (vis[i][j]) continue;
    vis[i][j] = true;

    for (int k = 0; k < 4; ++k) {
      size_t ni = i + di[k];
      size_t nj = j + dj[k];
      if (!(ni < h && nj < w)) continue;

      int m0 = b[i][j];
      int m1 = b[ni][nj];
      if (abs(m0-m1) <= 1 && !vis[ni][nj]) {
        q.emplace(ni, nj);
      }

      size_t nni = ni + di[k];
      size_t nnj = nj + dj[k];
      if (!(nni < h && nnj < w)) continue;

      int m2 = b[nni][nnj];
      if (m2 == m0 && m2 > m1 && !vis[nni][nnj]) {
        q.emplace(nni, nnj);
      }
    }
  }

  puts(vis[gy][gx]? "YES":"NO");
}
0