結果
問題 | No.124 門松列(3) |
ユーザー |
|
提出日時 | 2016-09-21 16:28:42 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 4 ms / 5,000 ms |
コード長 | 1,978 bytes |
コンパイル時間 | 821 ms |
コンパイル使用メモリ | 82,212 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-17 10:27:13 |
合計ジャッジ時間 | 1,749 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
#include <iostream>#include <vector>#include <queue>#include <cassert>#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))using namespace std;template <typename T, typename X> auto vectors(T a, X x) { return vector<T>(x, a); }template <typename T, typename X, typename Y, typename... Zs> auto vectors(T a, X x, Y y, Zs... zs) { auto cont = vectors(a, y, zs...); return vector<decltype(cont)>(x, cont); }const int inf = 1e9+7;const int dy[] = { -1, 1, 0, 0 };const int dx[] = { 0, 0, 1, -1 };struct state_t { int y, x, last, dist; };bool operator < (state_t const & a, state_t const & b) { return a.dist > b.dist; } // reversed, weakbool is_kadomatsu_sequence(int a, int b, int c) {if (a == 0) return true;return ((a < b and b > c) or (a > b and b < c)) and a != c;}int main() {int w, h; cin >> w >> h;vector<vector<int> > m = vectors(int(), h, w);repeat (y,h) repeat (x,w) cin >> m[y][x];repeat (y,h) repeat (x,w) assert (1 <= m[y][x] and m[y][x] <= 9);auto is_on_field = [&](int y, int x) { return 0 <= y and y < h and 0 <= x and x < w; };vector<vector<vector<int> > > dist = vectors(inf, h, w, 10);priority_queue<state_t> que; {state_t a;a.y = 0;a.x = 0;a.last = 0;a.dist = 0;que.push(a);}int ans = -1;while (not que.empty()) {state_t a = que.top(); que.pop();if (a.y == h-1 and a.x == w-1) {ans = a.dist;break;}repeat (i,4) {state_t b = a;b.y += dy[i];b.x += dx[i];b.last = m[a.y][a.x];b.dist += 1;if (not is_on_field(b.y, b.x)) continue;if (not is_kadomatsu_sequence(a.last, b.last, m[b.y][b.x])) continue;if (dist[b.y][b.x][b.last] != inf) continue;dist[b.y][b.x][b.last] = b.dist;que.push(b);}}cout << ans << endl;return 0;}