結果

問題 No.2731 Two Colors
ユーザー shobonvipshobonvip
提出日時 2024-04-19 22:53:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 255 ms / 3,000 ms
コード長 2,184 bytes
コンパイル時間 4,561 ms
コンパイル使用メモリ 262,860 KB
実行使用メモリ 11,696 KB
最終ジャッジ日時 2024-04-19 22:53:52
合計ジャッジ時間 8,383 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 245 ms
11,264 KB
testcase_01 AC 255 ms
11,136 KB
testcase_02 AC 247 ms
11,136 KB
testcase_03 AC 3 ms
5,376 KB
testcase_04 AC 9 ms
5,376 KB
testcase_05 AC 8 ms
5,376 KB
testcase_06 AC 25 ms
5,376 KB
testcase_07 AC 26 ms
5,408 KB
testcase_08 AC 5 ms
5,376 KB
testcase_09 AC 94 ms
9,596 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 103 ms
11,572 KB
testcase_12 AC 93 ms
9,448 KB
testcase_13 AC 88 ms
9,272 KB
testcase_14 AC 44 ms
6,368 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 AC 42 ms
6,208 KB
testcase_17 AC 58 ms
7,268 KB
testcase_18 AC 12 ms
5,376 KB
testcase_19 AC 46 ms
6,260 KB
testcase_20 AC 61 ms
7,492 KB
testcase_21 AC 13 ms
5,376 KB
testcase_22 AC 105 ms
10,280 KB
testcase_23 AC 27 ms
5,376 KB
testcase_24 AC 14 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 80 ms
8,576 KB
testcase_27 AC 105 ms
10,612 KB
testcase_28 AC 74 ms
8,188 KB
testcase_29 AC 24 ms
5,376 KB
testcase_30 AC 39 ms
6,532 KB
testcase_31 AC 29 ms
5,540 KB
testcase_32 AC 135 ms
11,696 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

//* ATCODER
#include<atcoder/all>
using namespace atcoder;
typedef modint998244353 mint;
//*/

/* BOOST MULTIPRECISION
#include<boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
//*/

typedef long long ll;

#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
#define rrep(i, s, n) for (int i = (int)(n)-1; i >= (int)(s); i--)

template <typename T> bool chmin(T &a, const T &b) {
	if (a <= b) return false;
	a = b;
	return true;
}

template <typename T> bool chmax(T &a, const T &b) {
	if (a >= b) return false;
	a = b;
	return true;
}

template <typename T> T max(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmax(ret, a[i]);
	return ret;
}

template <typename T> T min(vector<T> &a){
	assert(!a.empty());
	T ret = a[0];
	for (int i=0; i<(int)a.size(); i++) chmin(ret, a[i]);
	return ret;
}

template <typename T> T sum(vector<T> &a){
	T ret = 0;
	for (int i=0; i<(int)a.size(); i++) ret += a[i];
	return ret;
}


int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int h,w; cin >> h >> w;
	vector a(h,vector<int>(w));
	rep(i,0,h)rep(j,0,w)cin>>a[i][j];
	vector<priority_queue<tuple<int,int,int>>> pq(2);
	vector c(h,vector<int>(w,-1));
	
	auto add=[&](int i, int j){
		int col = c[i][j];
		for (auto[x,y]:{
			pair(i+1,j),pair(i-1,j),pair(i,j+1),pair(i,j-1)
		}){
			if(!(0<=x&&x<h))continue;
			if(!(0<=y&&y<w))continue;
			if(c[x][y]!=-1)continue;
			pq[col].push(make_tuple(-a[x][y],x,y));
		}
	};

	auto owari_hantei=[&](int i,int j){
		int col = c[i][j];
		for (auto[x,y]:{
			pair(i+1,j),pair(i-1,j),pair(i,j+1),pair(i,j-1)
		}){
			if(!(0<=x&&x<h))continue;
			if(!(0<=y&&y<w))continue;
			if(c[x][y]==(col^1))return true;
		}
		return false;
	};

	c[0][0]=0;
	c[h-1][w-1]=1;
	add(0,0);add(h-1,w-1);
	
	int cnt = 0;
	while(true){
		bool mode = 0;
		bool tt = 0;
		while(!pq[cnt%2].empty()){
			auto [val,x,y] = pq[cnt%2].top();
			pq[cnt%2].pop();
			if (c[x][y]==-1){
				c[x][y] = cnt%2;
				add(x,y);
				tt = owari_hantei(x,y);
				mode = 1;
				break;
			}
		}
		
		if(!mode)break;
		cnt++;
		if(tt)break;
	}
	cout << cnt << '\n';
}

0