結果

問題 No.424 立体迷路
ユーザー w10y26w10y26
提出日時 2017-07-03 14:03:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,947 bytes
コンパイル時間 865 ms
コンパイル使用メモリ 95,712 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-18 17:01:25
合計ジャッジ時間 1,916 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define ll long long
#define ffor(i, a, b) for (int i = (a); i < (b); i++)
#define rfor(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define SIZE 100001
#define MOD 1000000007
#define INF 100000000
using namespace std;
int h, w, sx, sy, gx, gy;
string b[51];
int visited[51][51];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
typedef pair<int, int> P;

bool bfs() {
  rep(i, h) rep(j, w) visited[i][j] = 0;
  queue<P> que;
  sx--;
  sy--;
  gx--;
  gy--;

  que.push(P(sy, sx));
  visited[sx][sy] = 1;
  int flag = 1;
  while (!que.empty()) {
    P p = que.front();

    que.pop();
    if (p.first == gy && p.second == gx) break;

    rep(i, 4) {
      int nx = p.second + dx[i];
      int ny = p.first + dy[i];
      if (0 <= ny && ny < w && 0 <= nx && nx < h && visited[nx][ny] == 0) {
        int h1 = b[nx][ny] - '0';
        int h2 = b[p.second][p.first] - '0';
        if (abs(h1 - h2) <= 1) {
          visited[nx][ny] = 1;
          que.push(P(ny, nx));
        }
      }
    }

    rep(i, 4) {
      int nx = p.second + 2 * dx[i];
      int ny = p.first + 2 * dy[i];
      if (0 <= ny && ny < w && 0 <= nx && nx < h && visited[nx][ny] == 0) {
        int h1 = b[nx][ny] - '0';
        int h2 = b[p.second][p.first] - '0';
        int half = b[(nx + p.second) / 2][(ny + p.first) / 2] - '0';
        if ((h1 == h2) && (h1 - half) > 0) {
          visited[nx][ny] = 1;
          que.push(P(ny, nx));
        }
      }
    }
  }
  return visited[gx][gy];
}

int main() {
  scanf("%d%d", &h, &w);
  scanf("%d%d%d%d", &sx, &sy, &gx, &gy);

  rep(i, h) cin >> b[i];

  if (bfs())
    printf("YES\n");
  else
    printf("NO\n");

  return 0;
}
0