結果
| 問題 | No.124 門松列(3) | 
| コンテスト | |
| ユーザー |  srjywrdnprkt | 
| 提出日時 | 2022-12-23 06:15:09 | 
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 37 ms / 5,000 ms | 
| コード長 | 1,482 bytes | 
| コンパイル時間 | 1,552 ms | 
| コンパイル使用メモリ | 142,060 KB | 
| 実行使用メモリ | 43,008 KB | 
| 最終ジャッジ日時 | 2024-11-18 03:56:38 | 
| 合計ジャッジ時間 | 2,807 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 26 | 
ソースコード
#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;
}
            
            
            
        