結果

問題 No.124 門松列(3)
ユーザー srjywrdnprkt
提出日時 2022-12-23 06:08:42
言語 C++17(clang)
(17.0.6 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,445 bytes
コンパイル時間 1,441 ms
コンパイル使用メモリ 142,056 KB
実行使用メモリ 43,008 KB
最終ジャッジ日時 2024-11-18 03:56:28
合計ジャッジ時間 3,065 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 23 WA * 3
権限があれば一括ダウンロードができます

ソースコード

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);
    assert(s.size() == 3);
    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