結果

問題 No.2731 Two Colors
ユーザー 沙耶花
提出日時 2024-04-19 21:42:20
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2,228 ms / 3,000 ms
コード長 1,231 bytes
コンパイル時間 4,318 ms
コンパイル使用メモリ 261,564 KB
最終ジャッジ日時 2025-02-21 03:59:06
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 1000000000000000001

int main(){
	
	int h,w;
	cin>>h>>w;
	vector<vector<int>> a(h,vector<int>(w));
	rep(i,h){
		rep(j,w)cin>>a[i][j];
	}
	map<int,pair<int,int>> mp;
	rep(i,h){
		rep(j,w)mp[a[i][j]] = make_pair(i,j);
	}
	vector ans(h,vector<int>(w,-1));
	
	vector<int> dx = {1,-1,0,0},dy = {0,0,1,-1};
	vector<priority_queue<int,vector<int>,greater<int>>> Q(2);
	rep(i,2){
		int x = 0,y = 0;
		if(i)x = h-1,y = w-1;
		ans[x][y] = i;
		rep(j,4){
			int xx = x+dx[j],yy = y+dy[j];
			//cout<<xx<<' '<<yy<<endl;
			if(xx<0||yy<0||xx>=h||yy>=w)continue;
			Q[i].push(a[xx][yy]);
		}
	}
	rep(_,10000000){
		int i = _%2;
		while(true){
			int u = Q[i].top();
			Q[i].pop();
			int x  =mp[u].first,y = mp[u].second;
			if(ans[x][y]!=-1)continue;
			
			ans[x][y] = i;
			rep(j,4){
				int xx = x+dx[j],yy = y+dy[j];
				if(xx<0||xx>=h||yy<0||yy>=w)continue;
				if(ans[xx][yy]!=-1 && ans[xx][yy]!=i){
					cout<<_+1<<endl;
					return 0;
				}
				Q[i].push(a[xx][yy]);
			}
			break;
		}
		
	}
	
	return 0;
}
0