結果

問題 No.124 門松列(3)
ユーザー kimiyukikimiyuki
提出日時 2016-09-21 16:28:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,978 bytes
コンパイル時間 768 ms
コンパイル使用メモリ 82,320 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-28 16:46:31
合計ジャッジ時間 1,635 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
using namespace std;
template <typename T, typename X> auto vectors(T a, X x) { return vector<T>(x, a); }
template <typename T, typename X, typename Y, typename... Zs> auto vectors(T a, X x, Y y, Zs... zs) { auto cont = vectors(a, y, zs...); return vector<decltype(cont)>(x, cont); }
const int inf = 1e9+7;
const int dy[] = { -1, 1, 0, 0 };
const int dx[] = { 0, 0, 1, -1 };
struct state_t { int y, x, last, dist; };
bool operator < (state_t const & a, state_t const & b) { return a.dist > b.dist; } // reversed, weak
bool is_kadomatsu_sequence(int a, int b, int c) {
    if (a == 0) return true;
    return ((a < b and b > c) or (a > b and b < c)) and a != c;
}
int main() {
    int w, h; cin >> w >> h;
    vector<vector<int> > m = vectors(int(), h, w);
    repeat (y,h) repeat (x,w) cin >> m[y][x];
    repeat (y,h) repeat (x,w) assert (1 <= m[y][x] and m[y][x] <= 9);
    auto is_on_field = [&](int y, int x) { return 0 <= y and y < h and 0 <= x and x < w; };
    vector<vector<vector<int> > > dist = vectors(inf, h, w, 10);
    priority_queue<state_t> que; {
        state_t a;
        a.y = 0;
        a.x = 0;
        a.last = 0;
        a.dist = 0;
        que.push(a);
    }
    int ans = -1;
    while (not que.empty()) {
        state_t a = que.top(); que.pop();
        if (a.y == h-1 and a.x == w-1) {
            ans = a.dist;
            break;
        }
        repeat (i,4) {
            state_t b = a;
            b.y += dy[i];
            b.x += dx[i];
            b.last = m[a.y][a.x];
            b.dist += 1;
            if (not is_on_field(b.y, b.x)) continue;
            if (not is_kadomatsu_sequence(a.last, b.last, m[b.y][b.x])) continue;
            if (dist[b.y][b.x][b.last] != inf) continue;
            dist[b.y][b.x][b.last] = b.dist;
            que.push(b);
        }
    }
    cout << ans << endl;
    return 0;
}
0