結果

問題 No.124 門松列(3)
ユーザー pekempey
提出日時 2015-08-20 15:28:08
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,799 bytes
コンパイル時間 1,371 ms
コンパイル使用メモリ 171,852 KB
実行使用メモリ 9,360 KB
最終ジャッジ日時 2024-07-18 10:54:01
合計ジャッジ時間 2,126 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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

#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < (a); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define repr(i, a) for (int i = (a) - 1; i >= 0; i--)
#define repr2(i, a, b) for (int i = (b) - 1; i >= (a); i--)
using namespace std;
typedef long long ll;
const ll inf = 1e9;
const ll mod = 1e9 + 7;
int W, H;
int M[111][111];
int dp[111][111][11][11]; // y, x, curr, prev
typedef tuple<int, int, int, int, int> P; // dist, y, x, curr, prev
int main() {
cin >> W >> H;
rep (i, H) rep (j, W) cin >> M[i][j];
rep (i, 111) rep (j, 111) rep (k, 11) rep (l, 11) dp[i][j][k][l] = inf;
priority_queue<P, vector<P>, greater<P>> q;
q.emplace(0, 0, 0, M[0][0], 0);
dp[0][0][M[0][0]][0] = 0;
while (!q.empty()) {
P p = q.top(); q.pop();
int y = get<1>(p);
int x = get<2>(p);
int curr = get<3>(p);
int prev = get<4>(p);
int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
rep (k, 4) {
int ny = y + dy[k];
int nx = x + dx[k];
if (ny < 0 || ny >= H || nx < 0 || nx >= W) continue;
int next = M[ny][nx];
if (prev == 0) {
if (curr == next) continue;
if (dp[ny][nx][next][curr] > dp[y][x][curr][prev] + 1) {
dp[ny][nx][next][curr] = dp[y][x][curr][prev] + 1;
q.emplace(dp[ny][nx][next][curr], ny, nx, next, curr);
}
} else {
if (prev == curr || prev == next || curr == next) continue;
if (prev > curr && curr < next || prev < curr && curr > next) {
if (dp[ny][nx][next][curr] > dp[y][x][curr][prev] + 1) {
dp[ny][nx][next][curr] = dp[y][x][curr][prev] + 1;
q.emplace(dp[ny][nx][next][curr], ny, nx, next, curr);
}
}
}
}
}
int ans = inf;
rep (k, 10) rep (l, 10) {
ans = min(ans, dp[H - 1][W - 1][k][l]);
}
if (ans == inf) ans = -1;
cout << ans << endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0