結果

問題 No.124 門松列(3)
ユーザー ttkkggwwttkkggww
提出日時 2022-05-07 23:57:00
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,328 bytes
コンパイル時間 4,344 ms
コンパイル使用メモリ 265,820 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 12:10:02
合計ジャッジ時間 5,484 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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();
}
0