結果

問題 No.124 門松列(3)
ユーザー ふーらくたる
提出日時 2016-09-16 14:29:05
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 2,222 bytes
コンパイル時間 699 ms
コンパイル使用メモリ 74,504 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-11-17 07:15:57
合計ジャッジ時間 1,836 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <iostream>
#include <tuple>
#include <functional>
#include <vector>
#include <queue>
using namespace std;
using State = tuple<int, int, int, int>; // (, , , )
const int MAX_W = 110;
const int MAX_H = 110;
const int INF = (1 << 28);
int dist[MAX_H][MAX_W][10];
int m[MAX_H][MAX_W];
int dr[4] = {1, 0, -1, 0},
dc[4] = {0, 1, 0, -1};
int w, h;
bool inside(int r, int c) {
return 0 <= r && r < h && 0 <= c && c < w;
}
int main() {
cin >> w >> h;
for (int r = 0; r < h; r++) {
for (int c = 0; c < w; c++) {
cin >> m[r][c];
}
}
for (int r = 0; r < h; r++) {
for (int c = 0; c < w; c++) {
for (int d = 0; d < 10; d++) {
dist[r][c][d] = INF;
}
}
}
priority_queue<State, vector<State>, greater<State>> que;
if (m[0][1] != m[0][0]) {
dist[0][1][m[0][0]] = 1;
que.push(State(1, 0, 1, m[0][0]));
}
if (m[1][0] != m[0][0]) {
dist[1][0][m[0][0]] = 1;
que.push(State(1, 1, 0, m[0][0]));
}
while (!que.empty()) {
State s = que.top(); que.pop();
int cost = get<0>(s),
row = get<1>(s),
col = get<2>(s),
prev = get<3>(s);
if (dist[row][col][prev] < cost) continue;
//
if (row == h - 1 && col == w - 1) {
cout << cost << endl;
return 0;
}
int cur = m[row][col];
for (int i = 0; i < 4; i++) {
int nr = row + dr[i], nc = col + dc[i];
if (inside(nr, nc)) {
//
int next = m[nr][nc];
if (prev == next || dist[nr][nc][cur] <= cost + 1) continue;
if (cur > prev && cur > next) {
dist[nr][nc][cur] = cost + 1;
que.push(State(cost + 1, nr, nc, cur));
} else if (cur < prev && cur < next) {
dist[nr][nc][cur] = cost + 1;
que.push(State(cost + 1, nr, nc, cur));
}
}
}
}
cout << -1 << endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0