結果
| 問題 | No.124 門松列(3) | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2022-05-07 23:35:42 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                WA
                                 
                             | 
| 実行時間 | - | 
| コード長 | 1,249 bytes | 
| コンパイル時間 | 4,061 ms | 
| コンパイル使用メモリ | 256,652 KB | 
| 最終ジャッジ日時 | 2025-01-29 05:00:28 | 
| ジャッジサーバーID (参考情報) | judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 18 WA * 8 | 
ソースコード
#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();
		//cout<<x<<' '<<y<<' '<<pre<<endl;
		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){
				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];
	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;
	for(int i = 0;i<h;i++){
		for(int j = 0;j<w;j++)cin >> G[i][j];
	}
	solve();
}
            
            
            
        