結果

問題 No.424 立体迷路
ユーザー treeonetreeone
提出日時 2016-09-22 23:11:20
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,654 bytes
コンパイル時間 1,272 ms
コンパイル使用メモリ 149,544 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-18 16:50:26
合計ジャッジ時間 2,392 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
using namespace std;
typedef pair<int, int> P;

int h, w;
int sy, sx, gy, gx;
int dy[4] = {1, -1, 0, 0};
int dx[4] = {0, 0, 1, -1};
int hy[4] = {2, -2, 0, 0};
int hx[4] = {0, 0, 2, -2};
char c[51][51];
bool f[51][51];

bool contain(int y, int x){
    return (0 <= y && y < h && 0 <= x && x < w);
}

signed main(){
    cin >> h >> w;
    cin >> sy >> sx >> gy >> gx;
    sy--; sx--; gy--; gx--;
    rep(i, 0, h){
        rep(j, 0, w){
            cin >> c[i][j];
            f[i][j] = false;
        }
    }
    queue<P> q;
    f[sy][sx] = true;
    q.push(P(sy, sx));
    while(q.size()){
        P p = q.front(); q.pop();
        int y = p.first, x = p.second;
        if(y == gy && x == gx) break;
        rep(i, 0, 4){
            int ny = y + dy[i];
            int nx = x + dx[i];
            if(contain(ny, nx) && f[ny][nx] == false){
                if(abs(c[y][x] - c[ny][nx]) <= 1){
                    f[ny][nx] = true;
                    q.push(P(ny, nx));
                }
            }
        }
        rep(i, 0, 4){
            int ny = y + hy[i];
            int nx = x + hx[i];
            int ty = y + dy[i];
            int tx = x + dx[i];
            if(contain(ny, nx) && f[ny][nx] == false){
                if(c[y][x] == c[ny][nx] && c[ty][tx] < c[y][x]){
                    f[ny][nx] = true;                    
                    q.push(P(ny, nx));
                }
            }
        }
    }
    cout << (f[gy][gx] ? "YES" : "NO") << endl;
}
0