結果

問題 No.424 立体迷路
ユーザー sekiya9311sekiya9311
提出日時 2016-09-22 22:48:36
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 3,043 bytes
コンパイル時間 1,317 ms
コンパイル使用メモリ 116,688 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 16:46:07
合計ジャッジ時間 2,440 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 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 #

#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>

#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a)-1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a)-1; (i) >= (b); (i)--)
#define PI acos(-1.0)
#define DEBUG(C) cerr << C << endl;
#define VI vector <int>
#define VII vector <VI>
#define VL vector <LL>
#define VLL vector <VL>
#define VD vector <double>
#define VDD vector <VD>
#define PII pair <int, int>
#define PDD pair <double, double>
#define PLL pair <LL, LL>
#define VPII vector <PII>
#define ALL(a) (a).begin(), (a).end()
#define SORT(a) sort(ALL(a))
#define REVERSE(a) reverse(ALL(a))
#define MP make_pair
#define FORE(a, b) for (auto &&a:b)
#define FIND(s, e) (s.find(e) != s.end())
#define EB emplace_back

using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;

int dx[] = {0, 0, -1, 1};
int dy[] = {1, -1, 0, 0};
int hx[] = {0, 0, -2, 2};
int hy[] = {2, -2, 0, 0};
int H, W;
PII start, goal;
vector <string> b;
vector <vector <bool>> is;
#define X first
#define Y second
bool inside(int x, int y) {
    bool res = true;
    if (x < 0 || H <= x) res = false;
    if (y < 0 || W <= x) res = false;
    return res;
}

bool dfs(PII now) {
    //cerr << now.X << " " << now.Y << endl;
    if (now == goal) {
        return true;
    }
    int nx = now.X, ny = now.Y;
    if (is[nx][ny]) {
        return false;
    }
    is[nx][ny] = true;
    REP(i, 4) {
        int ndx = nx + dx[i];
        int ndy = ny + dy[i];
        //cerr << ndx << " " << ndy << endl;
        if (!inside(ndx, ndy)) continue;
        if (abs(b[nx][ny] - b[ndx][ndy]) <= 1 && dfs({ndx, ndy})) {
            return true;
        }
    }
    REP(i, 4) {
        int ndx = nx + dx[i];
        int ndy = ny + dy[i];
        int nhx = nx + hx[i];
        int nhy = ny + hy[i];
        //cerr << nhx << " " << nhy << endl;
        if (!inside(nhx, nhy)) continue;
        if (abs(b[nx][ny] - b[nhx][nhy])) continue;
        if (b[nx][ny] <= b[ndx][ndy]) continue;
        if (dfs({nhx, nhy})) {
            return true;
        }
    }
    return false;
}

int main(void) {
    cin >> H >> W;
    b.resize(H);
    is.resize(H);
    cin >> start.X >> start.Y;
    cin >> goal.X >> goal.Y;
    start.X--;start.Y--;
    goal.X--;goal.Y--;
    REP(i, H) {
        cin >> b[i];
        is[i].resize(W, false);
    }
    if (dfs(start)) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }
}
0