結果
| 問題 | No.124 門松列(3) |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-02-26 21:44:57 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 4 ms / 5,000 ms |
| コード長 | 1,459 bytes |
| コンパイル時間 | 3,078 ms |
| コンパイル使用メモリ | 201,680 KB |
| 最終ジャッジ日時 | 2025-01-19 04:56:40 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
const int INF = 1<<28;
int w, h;
int bd[100][100];
int dist[100][100][10];
int dy[] = {0, 1, 0, -1};
int dx[] = {1, 0, -1, 0};
bool kadomatsu(int x, int y, int z) {
if (x == y || y == z || z == x) return false;
if (max({x, y, z}) == y) return true;
if (min({x, y, z}) == y) return true;
return false;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> w >> h;
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
cin >> bd[i][j];
queue<tuple<int, int, int> > q;
int s = bd[0][0];
for (int i = 0; i < h; i++)
for (int j = 0; j < w; j++)
for (int k = 0; k < 10; k++)
dist[i][j][k] = INF;
dist[0][1][s] = 1;
dist[1][0][s] = 1;
q.emplace(0, 1, s);
q.emplace(1, 0, s);
while (!q.empty()) {
int y, x, pre, d;
tie(y, x, pre) = q.front();
q.pop();
d = dist[y][x][pre];
for (int k = 0; k < 4; k++) {
int y2 = y + dy[k];
int x2 = x + dx[k];
if (x2 < 0 || x2 >= w) continue;
if (y2 < 0 || y2 >= h) continue;
if (kadomatsu(pre, bd[y][x], bd[y2][x2])) {
if (dist[y2][x2][bd[y][x]] == INF) {
dist[y2][x2][bd[y][x]] = d + 1;
q.emplace(y2, x2, bd[y][x]);
}
}
}
}
int ret = INF;
for (int i = 0; i < 10; i++)
ret = min(ret, dist[h-1][w-1][i]);
if (ret == INF)
cout << -1 << endl;
else
cout << ret << endl;
return 0;
}