結果
問題 | No.124 門松列(3) |
ユーザー |
|
提出日時 | 2022-05-07 23:57:00 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 18 ms / 5,000 ms |
コード長 | 1,328 bytes |
コンパイル時間 | 4,093 ms |
コンパイル使用メモリ | 256,604 KB |
最終ジャッジ日時 | 2025-01-29 05:01:04 |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 26 |
ソースコード
#include<bits/stdc++.h>using namespace std;#include<atcoder/all>using namespace atcoder;using ll = long long;int h,w;int G[100][100];int dist[100][100][10];using TUP = tuple<int,int,int>;void solve(){for(int i = 0;i<100;i++){for(int j = 0;j<100;j++){for(int k = 0;k<10;k++)dist[i][j][k] = 1000'000'000;}}queue<TUP> Q;dist[0][0][G[0][0]] = 0;Q.emplace(0,0,G[0][0]);int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};while(Q.size()){auto [x,y,pre] = Q.front();int cur_dist = dist[x][y][pre];Q.pop();for(int i = 0;i<4;i++){int nx = x + dx[i];int ny = y + dy[i];if(nx<0||nx>=h||ny<0||ny>=w)continue;int cur = G[x][y];int next = G[nx][ny];if(pre<cur&&cur<next)continue;if(pre>cur&&cur>next)continue;if(cur!=next&&pre!=next){if(dist[nx][ny][cur]>cur_dist+1){dist[nx][ny][cur] = cur_dist + 1;Q.emplace(nx,ny,cur);}}}}int ans = dist[h-1][w-1][0];/*cout<<dist[0][0][4]<<endl;cout<<dist[1][0][4]<<endl;cout<<dist[1][1][3]<<endl;*/for(int i = 0;i<10;i++){ans = min(dist[h-1][w-1][i],ans);}if(ans==1000'000'000)ans= -1;cout<<ans<<endl;}signed main(){cin.tie(nullptr);ios::sync_with_stdio(false);cin >> h >> w;swap(h,w);for(int i = 0;i<h;i++){for(int j = 0;j<w;j++)cin >> G[i][j];}solve();}