結果

問題 No.124 門松列(3)
ユーザー tottoripapertottoripaper
提出日時 2015-03-16 05:41:41
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,426 bytes
コンパイル時間 373 ms
コンパイル使用メモリ 50,296 KB
実行使用メモリ 433,644 KB
最終ジャッジ日時 2023-09-11 08:29:05
合計ジャッジ時間 12,937 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <queue>
#include <algorithm>

inline int index(int t, int x, int y, int p1, int p2){
    return x + 101 * (y + 101 * (p1 + 10 * (p2 + 10 * t)));
}

inline bool isKadomatsu(int a, int b, int c){
    if(a == b || b == c || c == a){return false;}
    if(b != std::max({a, b, c}) && b != std::min({a, b, c})){return false;}
    return true;
}

int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};

int main(){
    int W, H;
    scanf("%d %d", &W, &H);

    int map[100][100];
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            scanf("%d", &map[i][j]);
        }
    }
    
    std::queue<int> q;
    q.push(index(0, 0, 0, 0, map[0][0]));

    while(!q.empty()){
        int idx = q.front(); q.pop();
        int x = idx % 101,
            y = idx / 101 % 101,
            p1 = idx / 101 / 101 % 10,
            p2 = idx / 101 / 101 / 10 % 10,
            t = idx / 101 / 101 / 10 / 10;
        // printf("%d %d %d %d %d\n", x, y, p1, p2, t);

        if(x == W-1 && y == H-1){printf("%d\n", t); return 0;}
        if(t == 10000){break;}
        
        for(int i=0;i<4;i++){
            int nx = x + dx[i], ny = y + dy[i];
            if(nx < 0 || nx >= W || ny < 0 || ny >= H){
                continue;
            }

            if(t < 1 || isKadomatsu(p1, p2, map[ny][nx])){
                q.push(index(t+1, nx, ny, p2, map[ny][nx]));
            }
        }
    }
    puts("-1");
}
0