結果

問題 No.424 立体迷路
ユーザー daleksprinterdaleksprinter
提出日時 2018-09-13 22:20:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,818 bytes
コンパイル時間 1,704 ms
コンパイル使用メモリ 164,416 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-18 17:05:31
合計ジャッジ時間 2,852 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
 
#define rep(i,a,b) for(int i=a;i<b;i++)
#define int long long
const int inf = 100100100100100;

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int dx2[4] = {2,0,-2,0};
int dy2[4] = {0,2,0,-2};

int arr[50][50];
int h, w, sx, sy, gx, gy;
int visited[50][50];

int ctoi(const char c){
  switch(c){
    case '0': return 0;
    case '1': return 1;
    case '2': return 2;
    case '3': return 3;
    case '4': return 4;
    case '5': return 5;
    case '6': return 6;
    case '7': return 7;
    case '8': return 8;
    case '9': return 9;
    default : return -1;
  }
}

bool dfs(int x, int y){
    if(x == gy && y == gx) return true;

    if(visited[y][x] == 1){
        return false;
    }else{
        visited[y][x] = 1;
    }

    rep(i,0,4){
        int tx = x + dx[i]; 
        int ty = y + dy[i];


        if(-1 < tx && tx < w && -1 < ty && ty < h && -2 < arr[ty][tx]-arr[y][x] && arr[ty][tx] - arr[y][x] < 2){
            if(dfs(tx,ty)) return true;
        }
    }

    rep(i,0,4){
        int tx = x + dx2[i];
        int ty = y + dy2[i];
        int mx = x + dx[i]; 
        int my = y + dy[i];

        if(-1 < tx && tx < w && -1 < ty && ty < h && 
            -1 < mx && mx < w && -1 < my && my < h && 
                arr[ty][tx] == arr[y][x] && arr[y][x] > arr[my][mx]){
            if(dfs(tx,ty)) return true;
        }
    }

    return false;

}

signed main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    cin >> h >> w >> sx >> sy >> gx >> gy;
    sx--; sy--; gx--; gy--;

    rep(i,0,h){
        rep(j,0,w){
            char t;
            cin >> t;
            arr[i][j] = ctoi(t);
            visited[i][j] = 0;
        }
    }


    if(dfs(sy,sx)){
        cout << "YES" << endl;
    }else{
        cout << "NO" << endl;
    }

}

0