結果

問題 No.124 門松列(3)
ユーザー h_nosonh_noson
提出日時 2016-05-25 02:06:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 1,647 bytes
コンパイル時間 649 ms
コンパイル使用メモリ 74,192 KB
実行使用メモリ 7,396 KB
最終ジャッジ日時 2024-04-16 12:28:06
合計ジャッジ時間 1,803 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;

#define REP(i,s,e) for (i = s; i <= e; i++)
#define rep(i,n) REP (i,0,(int)(n)-1)
#define RREP(i,s,e) for (i = s; i >= e; i--)
#define rrep(i,n) RREP (i,(int)(n)-1,0)
#define INF (int)1e8
#define MOD (int)1e9+7

typedef long long ll;

bool is_kadomatsu(int a, int b, int c) {
    return a != c && (b < min(a,c) || b > max(a,c));
}

int dp[100][100][10][10];

int main(void) {
    int i, j, a, b, w, h;
    int m[100][100];
    queue<pair<pair<int,int>,pair<int,int>>> q;
    cin >> w >> h;
    rep (i,h) rep (j,w) cin >> m[i][j];
    rep (i,h) rep (j,w) rep (a,10) rep (b,10) dp[i][j][a][b] = INF;
    q.push(make_pair(make_pair(0,0),make_pair(0,m[0][0])));
    dp[0][0][0][m[0][0]] = 0;
    while (!q.empty()) {
        auto&& p = q.front();
        i = p.first.first;
        j = p.first.second;
        a = p.second.first;
        b = p.second.second;
        q.pop();
        int k;
        int dxy[4] = {-1,0,1,0};
        rep (k,4) {
            int ni = i + dxy[k];
            int nj = j + dxy[(k+1)%4];
            if (ni < 0 || ni >= h || nj < 0 || nj >= w) continue;
            if ((is_kadomatsu(a,b,m[ni][nj]) || !a) && dp[ni][nj][b][m[ni][nj]] > dp[i][j][a][b] + 1) {
                dp[ni][nj][b][m[ni][nj]] = dp[i][j][a][b] + 1;
                q.push(make_pair(make_pair(ni,nj),make_pair(b,m[ni][nj])));
            }
        }
    }
    int ans = INF;
    rep (a,10) rep (b,10) ans = min(ans,dp[h-1][w-1][a][b]);
    if (ans == INF)
        cout << -1 << endl;
    else 
        cout << ans << endl;
    return 0;
}
0