結果

問題 No.124 門松列(3)
ユーザー izuru_matsuuraizuru_matsuura
提出日時 2016-09-20 21:33:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 2,557 bytes
コンパイル時間 1,830 ms
コンパイル使用メモリ 177,356 KB
実行使用メモリ 9,088 KB
最終ジャッジ日時 2024-04-28 16:38:53
合計ジャッジ時間 2,809 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
9,088 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 8 ms
9,088 KB
testcase_04 AC 7 ms
9,088 KB
testcase_05 AC 7 ms
9,088 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 4 ms
5,504 KB
testcase_24 AC 5 ms
6,528 KB
testcase_25 AC 4 ms
6,528 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 7 ms
9,088 KB
testcase_29 AC 7 ms
9,088 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

namespace {

    typedef double real;
    typedef long long ll;

    template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {
        if (vs.empty()) return os << "[]";
        auto i = vs.begin();
        os << "[" << *i;
        for (++i; i != vs.end(); ++i) os << " " << *i;
        return os << "]";
    }
    template<class T> istream& operator>>(istream& is, vector<T>& vs) {
        for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;
        return is;
    }

    int W, H;
    vector<vector<int>> M;
    void input() {
        cin >> W >> H;
        M = vector<vector<int>>(H, vector<int>(W, 0));
        cin >> M;
    }

    const int dy[] = {-1, 0, 1, 0};
    const int dx[] = {0, 1, 0, -1};
    const int INF = 1<<28;

    struct S {
        int y, x, p, q;
        int c;
        S() {}
        S(int y, int x, int p, int q, int c) : y(y), x(x), p(p), q(q), c(c) {}
    };
    ostream& operator<<(ostream& os, const S& s) {
        return os << "S[(" << s.y << "," << s.x << " " << s.p << " " << s.q << ") -> " << s.c << "]";
    }

    void solve() {
        int D[H][W][12][12];
        for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) for (int k = 0; k < 12; k++) for (int l = 0; l < 12; l++) D[i][j][k][l] = INF;
        queue<S> Q;
        Q.emplace(0, 0, 0, 11, 0);
        Q.emplace(0, 0, 11, 0, 0);
        D[0][0][0][11] = 0;
        D[0][0][11][0] = 0;
        while (not Q.empty()) {
            S cur = Q.front(); Q.pop();
            //cout << cur << endl;
            for (int i = 0; i < 4; i++) {
                int ny = cur.y + dy[i];
                int nx = cur.x + dx[i];
                int np = M[cur.y][cur.x];
                int nq = cur.p;
                int nc = cur.c + 1;
                if (ny < 0 || ny >= H) continue;
                if (nx < 0 || nx >= W) continue;
                int cp = M[ny][nx];
                if ( (nq - cp) * (np - cp) >= 0 && (np - nq) * (cp - nq) >= 0 ) continue; // kadomatsu?
                int& cur_best = D[ny][nx][np][nq];
                if (cur_best > nc) {
                    cur_best = nc;
                    Q.emplace(ny, nx, np, nq, nc);
                }
            }
        }
        int ans = INF;
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= 9; j++) {
                ans = min(ans, D[H - 1][W - 1][i][j]);
            }
        }
        if (ans == INF) ans = -1;
        cout << ans << endl;
    }
}

int main() {
    input(); solve();
    return 0;
}

0