結果

問題 No.124 門松列(3)
ユーザー 沙耶花沙耶花
提出日時 2021-10-26 22:50:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,242 bytes
コンパイル時間 5,007 ms
コンパイル使用メモリ 261,924 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-15 22:40:40
合計ジャッジ時間 6,149 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint1000000007;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf 1000000000

bool check(int a,int b,int c){
	if(a==b||b==c||c==a)return false;
	if(min({a,b,c})==b||max({a,b,c})==b)return true;
	return false;
}

int main(){
	
	int w,h;
	cin>>w>>h;
	
	vector a(h,vector<int>(w));
	rep(i,h){
		rep(j,w)cin>>a[i][j];
	}
	queue<pair<pair<int,int>,int>> Q;
	vector dis(h,vector(w,vector<int>(4,Inf)));
	vector<int> dx = {1,0,-1,0},dy = {0,-1,0,1};
	dis[0][1][0] = 1;
	Q.emplace(make_pair(0,1),0);
	dis[1][0][3] = 1;
	Q.emplace(make_pair(1,0),3);
	
	while(Q.size()>0){
		int y = Q.front().first.first,x = Q.front().first.second;
		int d = Q.front().second;
		Q.pop();
		
		rep(i,4){
			int yy = y+dy[i],xx = x+dx[i],dd = i;
			if(yy<0||yy>=h||xx<0||xx>=w)continue;
			if(dis[yy][xx][dd]!=Inf)continue;
			int Y = a[y][x];
			int Z = a[yy][xx];
			int X = a[y-dy[d]][x-dx[d]];
			if(check(X,Y,Z)){
				dis[yy][xx][dd] = dis[y][x][d]+1;
				Q.emplace(make_pair(yy,xx),dd);
			}
		}
	}
	
	int ans = Inf;
	rep(i,4){
		ans = min(ans,dis.back().back()[i]);
	}
	if(ans==Inf)ans = -1;
	cout<<ans<<endl;
	
	
	return 0;
}
0