結果

問題 No.424 立体迷路
ユーザー Kenji-HKenji-H
提出日時 2016-09-22 22:44:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,841 bytes
コンパイル時間 1,150 ms
コンパイル使用メモリ 97,760 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-18 16:45:22
合計ジャッジ時間 1,862 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <unordered_set>
#include <unordered_map>

using namespace std;

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,b,e) for (int i=(int)(b); i<(int)(e); i++)
#define ALL(x) (x).begin(), (x).end()

int h, w;
int sx,sy,gx,gy;
int b[64][64];
bool vis[64][64];
int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};

bool onBoard(int x, int y, int h, int w) {
  if (x < 0 || x >= h) return false;
  if (y < 0 || y >= w) return false;
  return true;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> h >> w;
  cin >> sx >> sy >> gx >> gy;
  --sx, --sy, --gx, --gy;
  REP (i, h) {
    string s;
    cin >> s;
    REP (j, w)
      b[i][j] = s[j] - '0';
  }

  queue<pair<int, int> > Q;
  Q.push({sx, sy});
  vis[sx][sy] = true;
  while (!Q.empty()) {
    int x, y;
    tie(x, y) = Q.front();
    Q.pop();
    
    REP (k, 4) {
      int x2 = x + dx[k];
      int y2 = y + dy[k];
      if (!onBoard(x2, y2, h, w)) continue;
      if (abs(b[x][y] - b[x2][y2]) > 1) continue;
      if (vis[x2][y2]) continue;
      Q.push({x2, y2});
      vis[x2][y2] = true;
    }
    REP (k, 4) {
      int x2 = x + dx[k];
      int y2 = y + dy[k];
      int x3 = x + 2 * dx[k];
      int y3 = y + 2 * dy[k];
      if (!onBoard(x3, y3, h, w)) continue;
      if (b[x][y] != b[x3][y3] || b[x2][y2] > b[x][y]) continue;
      if (vis[x3][y3]) continue;
      Q.push({x3, y3});
      vis[x3][y3] = true;
    }
  }

  if (vis[gx][gy])
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  return 0;
}
0