結果

問題 No.124 門松列(3)
ユーザー face4face4
提出日時 2019-03-02 17:30:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,443 bytes
コンパイル時間 624 ms
コンパイル使用メモリ 76,580 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-05 16:55:52
合計ジャッジ時間 2,048 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<queue>
#include<vector>
using namespace std;
#define inRange(x,a,b) (a <= x && x < b)
int di[8] = {0,0,1,-1,1,1,-1,-1};
int dj[8] = {1,-1,0,0,1,-1,1,-1};

struct data{
    int ti, tj, dir, cost;
};

bool isK(int a, int b, int c){
    return a!=b && b!=c && c!=a && (b-a)*(b-c)>0;
}

int main(){
    int w, h;
    cin >> w >> h;

    int mat[h][w];
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            cin >> mat[i][j];
        }
    }

    int dp[h][w][4];
    for(int i = 0; i < h; i++)  for(int j = 0; j < w; j++)  for(int k = 0; k < 4; k++)  dp[i][j][k] = 1<<30;

    queue<data> q;
    if(mat[0][0] != mat[0][1])  q.push(data({0, 1, 0, 1}));
    if(mat[0][0] != mat[1][0])  q.push(data({1, 0, 2, 1}));

    while(!q.empty()){
        data p = q.front(); q.pop();
        int i = p.ti, j = p.tj, d = p.dir, c = p.cost;
        int pi = i-di[d], pj = j-dj[d];
        if(dp[i][j][d] <= c)    continue;
        dp[i][j][d] = c;
        for(int k = 0; k < 4; k++){
            int ni = i+di[k], nj = j+dj[k];
            if(!inRange(ni, 0, h) || !inRange(nj, 0, w))    continue;
            if(isK(mat[pi][pj], mat[i][j], mat[ni][nj]) && dp[ni][nj][k] > c+1){
                q.push(data({ni, nj, k, c+1}));
            }
        }
    }

    int ans = 1<<30;
    for(int i = 0; i < 4; i++)  ans = min(ans, dp[h-1][w-1][i]);
    cout << (ans == 1<<30 ? -1 : ans) << endl;

    return 0;
}
0