結果

問題 No.1572 XI
ユーザー KudeKude
提出日時 2021-06-27 14:19:06
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 270 ms / 2,000 ms
コード長 1,849 bytes
コンパイル時間 4,412 ms
コンパイル使用メモリ 267,340 KB
実行使用メモリ 28,276 KB
最終ジャッジ日時 2023-09-07 17:47:09
合計ジャッジ時間 11,601 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
26,888 KB
testcase_01 AC 9 ms
26,840 KB
testcase_02 AC 9 ms
26,828 KB
testcase_03 AC 8 ms
26,900 KB
testcase_04 AC 63 ms
27,144 KB
testcase_05 AC 135 ms
27,692 KB
testcase_06 AC 161 ms
27,584 KB
testcase_07 AC 12 ms
26,992 KB
testcase_08 AC 137 ms
27,488 KB
testcase_09 AC 68 ms
27,140 KB
testcase_10 AC 110 ms
27,332 KB
testcase_11 AC 17 ms
26,872 KB
testcase_12 AC 52 ms
27,184 KB
testcase_13 AC 19 ms
26,920 KB
testcase_14 AC 54 ms
27,124 KB
testcase_15 AC 18 ms
26,956 KB
testcase_16 AC 45 ms
27,036 KB
testcase_17 AC 12 ms
26,868 KB
testcase_18 AC 64 ms
27,428 KB
testcase_19 AC 145 ms
27,584 KB
testcase_20 AC 96 ms
27,524 KB
testcase_21 AC 166 ms
27,536 KB
testcase_22 AC 132 ms
27,400 KB
testcase_23 AC 11 ms
26,868 KB
testcase_24 AC 9 ms
26,896 KB
testcase_25 AC 8 ms
27,156 KB
testcase_26 AC 9 ms
26,860 KB
testcase_27 AC 8 ms
26,840 KB
testcase_28 AC 8 ms
26,892 KB
testcase_29 AC 9 ms
26,836 KB
testcase_30 AC 9 ms
26,868 KB
testcase_31 AC 9 ms
26,872 KB
testcase_32 AC 8 ms
26,948 KB
testcase_33 AC 9 ms
26,828 KB
testcase_34 AC 248 ms
27,980 KB
testcase_35 AC 244 ms
27,964 KB
testcase_36 AC 250 ms
27,968 KB
testcase_37 AC 259 ms
28,272 KB
testcase_38 AC 260 ms
28,112 KB
testcase_39 AC 256 ms
28,064 KB
testcase_40 AC 238 ms
28,168 KB
testcase_41 AC 253 ms
27,952 KB
testcase_42 AC 253 ms
27,920 KB
testcase_43 AC 248 ms
27,884 KB
testcase_44 AC 269 ms
28,276 KB
testcase_45 AC 269 ms
27,988 KB
testcase_46 AC 268 ms
28,012 KB
testcase_47 AC 11 ms
27,924 KB
testcase_48 AC 270 ms
27,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
#define rep(i,n)for (int i = 0; i < int(n); ++i)
#define rrep(i,n)for (int i = int(n)-1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
template<class T> void chmax(T& a, const T& b) {a = max(a, b);}
template<class T> void chmin(T& a, const T& b) {a = min(a, b);}
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
constexpr int INF = 1001001001;
int dist[1000][1000][6];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    rep(i, 1000) rep(j, 1000) rep(k, 6) dist[i][j][k] = INF;
    int h, w;
    cin >> h >> w;
    int sx, sy;
    cin >> sx >> sy;
    sx--, sy--;
    int gx, gy;
    cin >> gx >> gy;
    gx--, gy--;
    vector<string> s(h);
    rep(i, h) cin >> s[i];
    queue<tuple<int, int, int>> q;
    auto push = [&](int i, int j, int k, int d) {
        if (dist[i][j][k] == INF) {
            dist[i][j][k] = d;
            q.emplace(i, j, k);
        }
    };
    push(sx, sy, 4, 0);
    while(!q.empty()) {
        auto [i, j, k] = q.front(); q.pop();
        int nd = dist[i][j][k] + 1;
        rep(direction, 4) {
            int ni = i + dx[direction], nj = j + dy[direction];
            if (!(0 <= ni && ni < h && 0 <= nj && nj < w && s[ni][nj] == '.')) {
                continue;
            }
            int nk = (
                k == 4 ? direction :
                k == 5 ? direction ^ 2 :
                ((direction ^ k) & 1) ? k :
                direction == k ? 5 : 4
            );
            push(ni, nj, nk, nd);
        }
    }
    int ans = dist[gx][gy][4];
    if (ans == INF) ans = -1;
    cout << ans << '\n';
}
0