結果

問題 No.424 立体迷路
ユーザー tnakao0123tnakao0123
提出日時 2016-09-25 00:22:01
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,708 bytes
コンパイル時間 699 ms
コンパイル使用メモリ 88,856 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-18 16:54:59
合計ジャッジ時間 1,870 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 424.cc: No.424 立体迷路 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_H = 50;
const int MAX_W = 50;

const int dxs[] = {1, 0, -1, 0}, dys[] = {0, -1, 0, 1};

/* typedef */

typedef pair<int,int> pii;

/* global variables */

int flds[MAX_H][MAX_W];
bool ds[MAX_H][MAX_W];

/* subroutines */

/* main */

int main() {
  int h, w;
  cin >> h >> w;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;

  for (int x = 0; x < h; x++) {
    string s;
    cin >> s;
    for (int y = 0; y < w; y++) flds[x][y] = s[y] - '0';
  }

  ds[sx][sy] = true;
  queue<pii> q;
  q.push(pii(sx, sy));

  while (! q.empty()) {
    pii u = q.front(); q.pop();
    int &ux = u.first, &uy = u.second;
    if (ux == gx && uy == gy) break;
    int uf = flds[ux][uy];

    for (int di = 0; di < 4; di++) {
      int vx = ux + dxs[di], vy = uy + dys[di];
      if (vx >= 0 && vx < h && vy >= 0 && vy < w && ! ds[vx][vy]) {
	int &vf = flds[vx][vy];
	if (vf >= uf - 1 && vf <= uf + 1) {
	  ds[vx][vy] = true;
	  q.push(pii(vx, vy));
	}
      }

      int vx1 = vx + dxs[di], vy1 = vy + dys[di];
      if (vx1 >= 0 && vx1 < h && vy1 >= 0 && vy1 < w && ! ds[vx1][vy1] &&
	  flds[vx1][vy1] == uf && flds[vx][vy] < uf) {
	ds[vx1][vy1] = true;
	q.push(pii(vx1, vy1));
      }
    }
  }

  cout << (ds[gx][gy] ? "YES" : "NO") << endl;
  return 0;
}
0