結果

問題 No.424 立体迷路
ユーザー thedekopinthedekopin
提出日時 2018-07-22 11:39:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,902 bytes
コンパイル時間 506 ms
コンパイル使用メモリ 66,972 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 17:04:20
合計ジャッジ時間 1,565 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

int H, W;
int sx,sy,gx,gy;
string field[150];
int check[150][150];

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

void dfs(int sy, int sx){
    if(sx==gy-1&&sy==gx-1)return;
    for(int i=0;i<4;i++){
        if (sx + dx[i] >= 0 && sx + dx[i] < W && sy + dy[i] >= 0 && sy + dy[i] < H && !check[sy + dy[i]][sx + dx[i]])
        {
            if(field[sy][sx] - '0' == field[sy+dy[i]][sx+dx[i]] - '0'){
                check[sy + dy[i]][sx + dx[i]] = check[sy][sx] + 1;
                dfs(sy + dy[i], sx + dx[i]);
            }else if (field[sy + dy[i]][sx + dx[i]] - '0' - 1 == field[sy][sx] - '0'){
                check[sy + dy[i]][sx + dx[i]] = check[sy][sx] + 1;
                dfs(sy + dy[i], sx + dx[i]);
            }else if (field[sy + dy[i]][sx + dx[i]] - '0' < field[sy][sx] - '0'){
                if (field[sy + dy[i]][sx + dx[i]] - '0' == field[sy][sx] - '0' - 1){
                    check[sy + dy[i]][sx + dx[i]] = check[sy][sx] + 1;
                    dfs(sy + dy[i], sx + dx[i]);
                }else if (sx + dx[i] + dx[i] >= 0 && sx + dx[i] + dx[i] < W && sy + dy[i] + dy[i] >= 0 && sy + dy[i] + dy[i] < H && !check[sy + dy[i] + dy[i]][sx + dx[i] + dx[i]] && field[sy][sx] - '0' == field[sy + dy[i] + dy[i]][sx + dx[i] + dx[i]] - '0'){
                    check[sy + dy[i] + dy[i]][sx + dx[i] + dx[i]] = check[sy][sx] +1;
                    dfs(sy + dy[i] + dy[i], sx + dx[i] + dx[i]);
                }
            }

        }
    }
    return;
}

int main(){
    cin >> H >> W;
    cin>>sx>>sy>>gx>>gy;

    for(int i=0;i<H;i++){
        cin>>field[i];
    }
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            check[i][j] = 0;
        }
    }
    check[sx-1][sy-1] = 1;
    dfs(sx-1,sy-1);

    if(check[gx-1][gy-1]){
        cout<<"YES"<<endl;
    }else{
        cout<<"NO"<<endl;
    }
}
0