結果
問題 | No.124 門松列(3) |
ユーザー |
![]() |
提出日時 | 2016-09-20 21:33:58 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 7 ms / 5,000 ms |
コード長 | 2,557 bytes |
コンパイル時間 | 1,794 ms |
コンパイル使用メモリ | 176,628 KB |
実行使用メモリ | 9,144 KB |
最終ジャッジ日時 | 2024-11-17 10:13:32 |
合計ジャッジ時間 | 2,852 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
#include <bits/stdc++.h>using namespace std;namespace {typedef double real;typedef long long ll;template<class T> ostream& operator<<(ostream& os, const vector<T>& vs) {if (vs.empty()) return os << "[]";auto i = vs.begin();os << "[" << *i;for (++i; i != vs.end(); ++i) os << " " << *i;return os << "]";}template<class T> istream& operator>>(istream& is, vector<T>& vs) {for (auto it = vs.begin(); it != vs.end(); it++) is >> *it;return is;}int W, H;vector<vector<int>> M;void input() {cin >> W >> H;M = vector<vector<int>>(H, vector<int>(W, 0));cin >> M;}const int dy[] = {-1, 0, 1, 0};const int dx[] = {0, 1, 0, -1};const int INF = 1<<28;struct S {int y, x, p, q;int c;S() {}S(int y, int x, int p, int q, int c) : y(y), x(x), p(p), q(q), c(c) {}};ostream& operator<<(ostream& os, const S& s) {return os << "S[(" << s.y << "," << s.x << " " << s.p << " " << s.q << ") -> " << s.c << "]";}void solve() {int D[H][W][12][12];for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) for (int k = 0; k < 12; k++) for (int l = 0; l < 12; l++) D[i][j][k][l] = INF;queue<S> Q;Q.emplace(0, 0, 0, 11, 0);Q.emplace(0, 0, 11, 0, 0);D[0][0][0][11] = 0;D[0][0][11][0] = 0;while (not Q.empty()) {S cur = Q.front(); Q.pop();//cout << cur << endl;for (int i = 0; i < 4; i++) {int ny = cur.y + dy[i];int nx = cur.x + dx[i];int np = M[cur.y][cur.x];int nq = cur.p;int nc = cur.c + 1;if (ny < 0 || ny >= H) continue;if (nx < 0 || nx >= W) continue;int cp = M[ny][nx];if ( (nq - cp) * (np - cp) >= 0 && (np - nq) * (cp - nq) >= 0 ) continue; // kadomatsu?int& cur_best = D[ny][nx][np][nq];if (cur_best > nc) {cur_best = nc;Q.emplace(ny, nx, np, nq, nc);}}}int ans = INF;for (int i = 1; i <= 9; i++) {for (int j = 1; j <= 9; j++) {ans = min(ans, D[H - 1][W - 1][i][j]);}}if (ans == INF) ans = -1;cout << ans << endl;}}int main() {input(); solve();return 0;}