結果

問題 No.424 立体迷路
ユーザー 🍡yurahuna🍡yurahuna
提出日時 2016-09-29 22:29:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,104 bytes
コンパイル時間 2,034 ms
コンパイル使用メモリ 179,612 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 16:56:09
合計ジャッジ時間 2,932 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(v) for(auto&& x : v){cout << x << " ";} cout << endl
#define printVV(vv) for(auto&& v : vv){for(auto&& x : v){cout << x << " ";}cout << endl;}
#define printP(p) cout << p.first << " " << p.second << endl
#define printVP(vp) for(auto&& p : vp) printP(p);

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<Pii> vp;
const int inf = 1e9;
const int mod = 1e9 + 7;

int H, W;
int sx, sy, gx, gy;
vector<string> s;

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

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

int bfs() {
    queue<pair<int, int>> que;
    que.emplace(sx, sy);
    vector<vector<int>> d(H, vector<int>(W, inf));
    d[sx][sy] = 0;

    while (!que.empty()) {
        int x, y;
        tie(x, y) = que.front(); que.pop();
        if (x == gx && y == gy)
             break;

        rep(k, 4) {
            int nx = x + dx[k], ny = y + dy[k];
            int nnx = x + 2 * dx[k], nny = y + 2 * dy[k];

            if (inside(nx, ny) && d[nx][ny] == inf && abs(s[nx][ny] - s[x][y]) <= 1) {
                d[nx][ny] = d[x][y] + 1;
                que.emplace(nx, ny);
            }

            if (inside(nnx, nny) && d[nnx][nny] == inf && s[nnx][nny] == s[x][y] && s[nx][ny] < s[x][y]) {
                d[nnx][nny] = d[x][y] + 1;
                que.emplace(nnx, nny);
            }
        }
    }

    return (d[gx][gy] == inf ? -1 : d[gx][gy]);
}

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

    cin >> H >> W;
    cin >> sx >> sy >> gx >> gy;
    sx--, sy--, gx--, gy--;
    s.resize(H);
    rep(i, H) cin >> s[i];

    cout << (bfs() != -1 ? "YES" : "NO") << endl;
}
0