結果
問題 |
No.124 門松列(3)
|
ユーザー |
|
提出日時 | 2015-01-21 20:46:07 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 14 ms / 5,000 ms |
コード長 | 1,763 bytes |
コンパイル時間 | 1,049 ms |
コンパイル使用メモリ | 114,284 KB |
実行使用メモリ | 11,136 KB |
最終ジャッジ日時 | 2024-06-22 23:33:38 |
合計ジャッジ時間 | 1,842 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
#include <cstdio> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <limits> #include <climits> #include <cfloat> #include <functional> using namespace std; int dy[] = {-1, 1, 0, 0}; int dx[] = {0, 0, 1, -1}; int main() { int w, h; cin >> w >> h; vector<vector<int> > m(h+2, vector<int>(w+2, -1)); for(int y=1; y<=h; ++y){ for(int x=1; x<=w; ++x){ cin >> m[y][x]; } } vector<vector<vector<vector<bool> > > > check(h+2, vector<vector<vector<bool> > >(w+2, vector<vector<bool> >(10, vector<bool>(10, false)))); queue<tuple<int, int, int, int> > q; check[1][1][0][m[1][1]] = 1; q.push(make_tuple(1, 1, 0, m[1][1])); int ret = 0; while(!q.empty()){ int n = q.size(); while(--n >= 0){ int y, x, a, b; tie(y, x, a, b) = q.front(); q.pop(); if(y == h && x == w){ cout << ret << endl; return 0; } for(int i=0; i<4; ++i){ int y2 = y + dy[i]; int x2 = x + dx[i]; int c = m[y2][x2]; if(c == -1) continue; if(a != 0 && !(a != c && ((a < b && b > c) || (a > b && b < c)))) continue; if(!check[y2][x2][b][c]){ check[y2][x2][b][c] = true; q.push(make_tuple(y2,x2, b, c)); } } } ++ ret; } cout << -1 << endl; return 0; }