結果

問題 No.424 立体迷路
ユーザー DrafearDrafear
提出日時 2016-09-22 22:41:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,431 bytes
コンパイル時間 1,510 ms
コンパイル使用メモリ 158,388 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-18 16:44:17
合計ジャッジ時間 2,626 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i,a) for (auto& i : a)
#define FOR(i,a,b) for (ll i=(a);i<(b);i++)
#define RFOR(i,a,b) for (ll i=(b)-1;i>=(a);i--)
#define REP(i,n) for (ll i=0;i<(n);i++)
#define RREP(i,n) for (ll i=(n)-1;i>=0;i--)
#define debug(x) cout<<#x<<": "<<x<<endl
#define pb push_back
#define ALL(a) (a).begin(),(a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    EACH(x,vec) is >> x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& vec) {
    REP(i,vec.size()) {
        if (i) os << " ";
        os << vec[i];
    }
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector< vector<T> >& vec) {
    REP(i,vec.size()) {
        if (i) os << endl;
        os << vec[i];
    }
    return os;
}

int H, W;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};
bool inRange(int x, int y) {
    return 0 <= x && x < W && 0 <= y && y < H;
}
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    cin >> H >> W;
    int sx, sy, gx, gy; cin >> sy >> sx >> gy >> gx; --sx, --sy, --gx, --gy;
    vector< vector<int> > h(H, vector<int>(W));
    REP(y, H) {
        string s; cin >> s;
        REP(x, W) {
            h[y][x] = s[x]-'0';
        }
    }
    vector< vector<int> > dist(H, vector<int>(W, inf)); dist[sy][sx] = 0;
    queue<P> Q; Q.push({sx, sy});
    while ( !Q.empty() ) {
        P p = Q.front(); Q.pop();
        int x, y; tie(x, y) = p;
        vector<P> nxt;
        REP(d, 4) {
            int nx = x + dx[d], ny = y + dy[d];
            if ( inRange(nx, ny) && abs(h[y][x] - h[ny][nx]) <= 1 ) {
                nxt.pb({nx, ny});
            }
        }
        REP(d, 4) {
            int nx = x + dx[d]*2, ny = y + dy[d]*2;
            if ( inRange(nx, ny) && h[y][x] == h[ny][nx] && h[y+dy[d]][x+dx[d]] <= h[y][x] ) {
                nxt.pb({nx, ny});
            }
        }
        EACH(np, nxt) {
            int nx, ny; tie(nx, ny) = np;
            if ( dist[y][x]+1 < dist[ny][nx] ) {
                dist[ny][nx] = dist[y][x]+1;
                Q.push({nx, ny});
            }
        }
    }
    cout << (dist[gy][gx] == inf ? "NO" : "YES") << endl;
}
0