結果

問題 No.124 門松列(3)
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-05-22 16:32:39
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,725 bytes
コンパイル時間 1,569 ms
コンパイル使用メモリ 172,788 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 07:10:42
合計ジャッジ時間 2,743 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,812 KB
testcase_01 AC 1 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,944 KB
testcase_05 AC 4 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 2 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 2 ms
6,940 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 4 ms
6,940 KB
testcase_29 AC 4 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.05.22 16:32:32
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;

bool isKadomatsuSequence(int a,int b,int c) {
    if (a < b && b > c && a != c) {
        return true;
    }
    if (a > b && b < c && a != c) {
        return true;
    }
    return false;
}
int main() {
    int w,h;cin >> w >> h;
    vector<vector<int>> m(h,vector<int>(w));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cin >> m[i][j];
        }
    }
    const int dx[] = {0,1,0,-1},dy[] = {1,0,-1,0};
    constexpr int INF = 1e9 + 6;
    vector<vector<vector<int>>> dp(h,vector<vector<int>>(w,vector<int>(4,INF)));
    queue<tuple<int,int,int>> que;
    if (m[0][0] != m[0][1]) {
        dp[0][1][0] = 1;
        que.push(make_tuple(0,1,0));
    }
    if (m[0][0] != m[1][0]) {
        dp[1][0][1] = 1;
        que.push(make_tuple(1,0,1));
    }
    while(!que.empty()) {
        auto p = que.front(); que.pop();
        int nx = get<0>(p);
        int ny = get<1>(p);
        int d = get<2>(p);
        int num2 = m[nx][ny];
        int num3 = m[nx-dx[d]][ny-dy[d]];
        for (int k = 0; k < 4; k++) {
            int x = nx + dx[k], y = ny + dy[k];
            if (x >= 0 && x < h && y >= 0 && y < w && isKadomatsuSequence(m[x][y],num2,num3) && dp[x][y][k] > dp[nx][ny][d] + 1) {
                dp[x][y][k] = dp[nx][ny][d] + 1;
                que.push(make_tuple(x,y,k));
            }
        }
    }
    int ans = INF;
    for (int k = 0; k < 4; k++) {
        ans = min(ans,dp[h-1][w-1][k]);
    }
    if (ans == INF) {
        cout << -1 << endl;
    }
    else {
        cout << ans << endl;
    }
    return 0;
}
0