結果

問題 No.124 門松列(3)
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2022-12-23 06:15:09
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 34 ms / 5,000 ms
コード長 1,482 bytes
コンパイル時間 1,277 ms
コンパイル使用メモリ 140,392 KB
実行使用メモリ 43,008 KB
最終ジャッジ日時 2024-04-29 04:11:59
合計ジャッジ時間 2,420 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
43,008 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 34 ms
43,008 KB
testcase_04 AC 32 ms
43,008 KB
testcase_05 AC 32 ms
43,008 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 1 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 4 ms
6,272 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 3 ms
5,376 KB
testcase_23 AC 12 ms
17,536 KB
testcase_24 AC 18 ms
24,448 KB
testcase_25 AC 18 ms
24,448 KB
testcase_26 AC 7 ms
10,624 KB
testcase_27 AC 5 ms
7,936 KB
testcase_28 AC 33 ms
43,008 KB
testcase_29 AC 33 ms
43,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <cassert>

using namespace std;

bool judge(int n){
    if (n < 100) return 1;
    string s = to_string(n);
    if (s[0] == s[1] || s[1] == s[2] || s[0] == s[2]) return 0;
    return (s[0] > s[1] && s[1] < s[2]) || (s[0] < s[1] && s[1] > s[2]);
}

int main(){

    int H, W, h, w, nh, nw, n, nn, mi=1e9;
    cin >> W >> H;
    
    queue<tuple<int, int, int>> que;
    int dx[4] = {1, 0, -1, 0};
    int dy[4] = {0, 1, 0, -1};
    vector<vector<vector<int>>> dist(H, vector(W, vector<int>(1000, -1)));
    vector<vector<int>> field(H, vector<int>(W));

    for (int i=0; i < H; i++){
        for (int j=0; j<W; j++) cin >> field[i][j];
    }

    dist[0][0][field[0][0]] = 0;
    que.push({0, 0, field[0][0]});

    while(!que.empty()){
        tie(h, w, n) = que.front();
        que.pop();
        for (int dir=0; dir < 4; dir++){
            nh = h + dx[dir];
            nw = w + dy[dir];
            if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
            nn = (n % 100) * 10 + field[nh][nw];
            if (!judge(nn)) continue;
            if (dist[nh][nw][nn] != -1) continue;
            dist[nh][nw][nn] = dist[h][w][n] + 1;
            que.push({nh, nw, nn});
        }
    }

    for (int i=0; i<1000; i++) if (dist[H-1][W-1][i] >= 0) mi = min(mi, dist[H-1][W-1][i]);

    cout << (mi == 1e9 ? -1 : mi) << endl;

    return 0;
}
0