結果

問題 No.124 門松列(3)
ユーザー tottoripapertottoripaper
提出日時 2015-03-16 06:19:12
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 2,547 bytes
コンパイル時間 398 ms
コンパイル使用メモリ 51,368 KB
実行使用メモリ 6,984 KB
最終ジャッジ日時 2023-09-11 08:29:28
合計ジャッジ時間 1,639 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
6,980 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 6 ms
6,984 KB
testcase_04 AC 5 ms
6,792 KB
testcase_05 AC 6 ms
6,928 KB
testcase_06 AC 1 ms
4,384 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 3 ms
4,584 KB
testcase_24 AC 3 ms
5,104 KB
testcase_25 AC 4 ms
5,396 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 5 ms
6,912 KB
testcase_29 AC 5 ms
6,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// ナニソレイミワカンナイ

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

class counter_iterator{
private:
    int count;
public:
    counter_iterator() : count(0){}
    counter_iterator(int n) : count(n){}
    bool operator==(const counter_iterator& rhs) const{
        return count == rhs.count;
    }
    bool operator!=(const counter_iterator& rhs) const{
        return count != rhs.count;
    }
    int& operator*(){
        return count;
    }
    counter_iterator& operator++(){
        ++count;
        return *this;
    }
};

class loop{
private:
    int b, e;
public:
    loop(int m) : b(0), e(m){}
    loop(int n, int m) : b(n), e(m){}
    counter_iterator begin() const{
        return counter_iterator(b);
    }
    counter_iterator end() const{
        return counter_iterator(e);
    }
};

inline long long index(int t, int x, int y, int p1, int p2){
    return 1ll * x + 101ll * (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 d[100][100][10][10];

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]);
        }
    }

    for(int i : loop(W)){
        for(int j : loop(H)){
            for(int k : loop(10)){
                for(int l : loop(10)){
                    d[i][j][k][l] = 252521;
                }
            }
        }
    }
    
    std::queue<long long> q;
    q.push(index(0, 0, 0, 0, map[0][0]));
    d[0][0][0][map[0][0]] = 0;

    while(!q.empty()){
        long long 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;

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

            }else{
                continue;
            }

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