結果

問題 No.424 立体迷路
ユーザー tetsuzuki1115tetsuzuki1115
提出日時 2018-03-04 17:11:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,696 bytes
コンパイル時間 1,053 ms
コンパイル使用メモリ 92,552 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-18 17:03:08
合計ジャッジ時間 2,112 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <math.h>
#include <cmath>
#include <limits.h>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <functional>
#include <stdio.h>
using namespace std;

long long MOD = 1000000007;

int H,W;
vector<string> V;
vector< vector<int> > D;

struct Point {
    Point(){};
    Point( int a, int b ){ x = a; y = b; };
    int x;
    int y;
};

Point S, G;

int dir[4][2] = { {1,0}, {0,1}, {-1,0}, {0,-1} };

bool isOK( int x, int y ) {
    return x >= 0 && y >= 0 && x < W && y < H;
}

bool func( Point p ) {
    // cout << p.x << " " << p.y << endl;

    if ( p.x == G.x && p.y == G.y ) { return true; }

    D[p.y][p.x] = 1;

    Point np;
    for ( int i = 0; i < 4; i++ ) {
        int nx1 = p.x + dir[i][0];
        int ny1 = p.y + dir[i][1];
        np.x = nx1;
        np.y = ny1;
        if (isOK(nx1,ny1) && D[ny1][nx1] == 0 && ( abs( V[p.y][p.x]-V[ny1][nx1] ) <= 1 ) && func( np ) ) {
            return true;
        }
        int nx2 = p.x + dir[i][0]*2;
        int ny2 = p.y + dir[i][1]*2;
        np.x = nx2;
        np.y = ny2;
        if ( isOK(nx2,ny2) && D[ny2][nx2] == 0 && V[p.y][p.x] == V[ny2][nx2] && V[p.y][p.x] > V[ny1][nx1] && func( np ) ) {
            return true;
        }
    }
    return false;
}

int main() {
    
    cin >> H >> W;
    int sx,sy,gx,gy;
    cin >> sy >> sx >> gy >> gx;
    sx--; sy--; gx--; gy--;
    S.x = sx; S.y = sy;
    G.x = gx; G.y = gy;
    
    V.resize(H);
    D.resize(H);    
    for ( int i = 0; i < H; i++ ) {
        cin >> V[i];
        D[i].resize(W,0);
    }

    cout << ( func( S ) ? "YES" : "NO" ) << endl;


    return 0;
}
0