結果
| 問題 | No.124 門松列(3) |
| コンテスト | |
| ユーザー |
siman
|
| 提出日時 | 2022-07-04 16:41:57 |
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 19 ms / 5,000 ms |
| コード長 | 1,984 bytes |
| コンパイル時間 | 5,095 ms |
| コンパイル使用メモリ | 144,756 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-12-14 07:38:41 |
| 合計ジャッジ時間 | 6,140 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
struct Node {
int y;
int x;
vector<int> path;
Node(int y = -1, int x = -1) {
this->y = y;
this->x = x;
this->path.clear();
}
bool operator>(const Node &n) const {
return path.size() > n.path.size();
}
};
const int DY[4] = {-1, 0, 1, 0};
const int DX[4] = {0, 1, 0, -1};
bool is_kadomatsu(int a1, int a2, int a3) {
if (a1 == a2) return false;
if (a1 == a3) return false;
if (a2 == a3) return false;
if (a1 < a2 && a2 < a3) return false;
if (a1 > a2 && a2 > a3) return false;
return true;
}
int main() {
int W, H;
cin >> W >> H;
int M[H][W];
for (int y = 0; y < H; ++y) {
for (int x = 0; x < W; ++x) {
cin >> M[y][x];
}
}
priority_queue <Node, vector<Node>, greater<Node>> pque;
Node root(0, 0);
root.path.push_back(M[0][0]);
pque.push(root);
bool visited[H][W][10];
memset(visited, false, sizeof(visited));
while (not pque.empty()) {
Node node = pque.top();
pque.pop();
int l = node.path.size();
if (l >= 2) {
if (visited[node.y][node.x][node.path[l - 2]]) continue;
visited[node.y][node.x][node.path[l - 2]] = true;
}
if (node.y == H - 1 && node.x == W - 1) {
cout << node.path.size() - 1 << endl;
return 0;
}
for (int dir = 0; dir < 4; ++dir) {
int ny = node.y + DY[dir];
int nx = node.x + DX[dir];
if (ny < 0 || nx < 0 || H <= ny || W <= nx) continue;
Node next(ny, nx);
next.path = node.path;
next.path.push_back(M[ny][nx]);
int l = next.path.size();
if (l >= 3) {
if (not is_kadomatsu(next.path[l - 1], next.path[l - 2], next.path[l - 3])) continue;
}
pque.push(next);
}
}
cout << -1 << endl;
return 0;
}
siman