結果

問題 No.124 門松列(3)
ユーザー mayoko_mayoko_
提出日時 2015-01-12 14:10:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,947 bytes
コンパイル時間 703 ms
コンパイル使用メモリ 77,652 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-04 05:03:41
合計ジャッジ時間 1,952 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>

#define INF 1000000000

using namespace std;
typedef long long ll;

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

const int MAXH = 110;
const int MAXW = 110;
int M[MAXH][MAXW];
int d[12][MAXH][MAXW];

struct state{
    int before;
    int cost;
    int y;
    int x;
};

bool isKM(int a, int b, int c) {
    if (a == 0 || b == 0 || c == 0) return true;
    if (a == b || b == c || c == a) return false;
    if (b < a && b < c) return true;
    if (b > a && b > c) return true;
    return false;
}

int main(void) {
    int W, H;
    cin >> W >> H;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> M[i][j];
            for (int k = 0; k < 12; k++) {
                d[k][i][j] = INF;
            }
        }
    }
    d[0][0][0] = 0;
    queue<state> que;
    state s; s.before = 0; s.cost = 0; s.y = 0; s.x = 0;
    que.push(s);
    while (!que.empty()) {
        state now = que.front(); que.pop();
        for (int k = 0; k < 4; k++) {
            int nx = now.x + dx[k];
            int ny = now.y + dy[k];
            if (nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
            if (!isKM(now.before, M[now.y][now.x], M[ny][nx])) continue;
            if (d[M[now.y][now.x]][ny][nx] < INF) continue;
            d[M[now.y][now.x]][ny][nx] = now.cost + 1;
            state next;
            next.before = M[now.y][now.x];
            next.cost = now.cost + 1;
            next.y = ny;
            next.x = nx;
            que.push(next);
        }
    }
    int ans = INF;
    for (int i = 0; i < 12; i++) {
        ans = min(ans, d[i][H-1][W-1]);
    }
    if (ans == INF) ans = -1;
    cout << ans << endl;
    return 0;
}
0