結果

問題 No.424 立体迷路
ユーザー confconf
提出日時 2016-09-22 22:46:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,044 bytes
コンパイル時間 517 ms
コンパイル使用メモリ 66,832 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-18 16:45:49
合計ジャッジ時間 1,694 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

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

using namespace std;
int height[50][50];
int sx,sy,gx,gy,H,W;
bool visited[50][50];

bool valid(int x, int y){
    return x>=0&&x<H&&y>=0&&y<W&&!visited[y][x];
}

bool rec(int x, int y){
    visited[y][x]=true;
    if(x==gx&&y==gy){
        return true;
    }
    int h=height[y][x];
    // cout<<x<<' '<<y<<' '<<h<<endl;
    int d[5]={0,1,0,-1,0};
    for(int i=0;i<4;i++){
        if(valid(x+d[i],y+d[i+1])&&abs(height[y+d[i+1]][x+d[i]]-h)<=1){
            if(rec(x+d[i],y+d[i+1])) return true;
        }
        if(valid(x+2*d[i],y+2*d[i+1])&&height[y+2*d[i+1]][x+2*d[i]]==h&&height[y+d[i+1]][x+d[i]]<h){
            if(rec(x+2*d[i],y+2*d[i+1])) return true;
        }
    }
    return false;
}

int main(){
    cin>>H>>W;
    cin>>sx>>sy>>gx>>gy;
    sx--;sy--;gx--;gy--;
    for(int i=0;i<H;i++){
        string s;
        cin>>s;
        for(int j=0;j<W;j++){
            height[j][i]=s[j]-'0';
        }
    }
    if(rec(sx,sy)){
        cout<<"YES"<<endl;
    }else{
        cout<<"NO"<<endl;
    }

    return 0;
}
0