結果

問題 No.2731 Two Colors
ユーザー kwm_tkwm_t
提出日時 2024-04-20 13:52:52
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 254 ms / 3,000 ms
コード長 2,127 bytes
コンパイル時間 3,411 ms
コンパイル使用メモリ 286,028 KB
実行使用メモリ 12,108 KB
最終ジャッジ日時 2024-04-20 13:53:06
合計ジャッジ時間 7,565 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 232 ms
11,008 KB
testcase_01 AC 236 ms
11,136 KB
testcase_02 AC 254 ms
11,008 KB
testcase_03 AC 3 ms
6,940 KB
testcase_04 AC 9 ms
6,944 KB
testcase_05 AC 8 ms
6,940 KB
testcase_06 AC 25 ms
6,944 KB
testcase_07 AC 27 ms
6,940 KB
testcase_08 AC 5 ms
6,944 KB
testcase_09 AC 93 ms
9,588 KB
testcase_10 AC 3 ms
6,940 KB
testcase_11 AC 109 ms
12,108 KB
testcase_12 AC 96 ms
9,452 KB
testcase_13 AC 89 ms
9,140 KB
testcase_14 AC 44 ms
6,944 KB
testcase_15 AC 6 ms
6,940 KB
testcase_16 AC 41 ms
6,940 KB
testcase_17 AC 58 ms
7,132 KB
testcase_18 AC 11 ms
6,940 KB
testcase_19 AC 43 ms
6,940 KB
testcase_20 AC 60 ms
7,488 KB
testcase_21 AC 12 ms
6,940 KB
testcase_22 AC 125 ms
10,144 KB
testcase_23 AC 27 ms
6,940 KB
testcase_24 AC 14 ms
6,940 KB
testcase_25 AC 3 ms
6,940 KB
testcase_26 AC 79 ms
8,448 KB
testcase_27 AC 98 ms
10,316 KB
testcase_28 AC 65 ms
8,188 KB
testcase_29 AC 21 ms
6,940 KB
testcase_30 AC 36 ms
6,940 KB
testcase_31 AC 25 ms
6,940 KB
testcase_32 AC 123 ms
11,500 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 AC 1 ms
6,944 KB
testcase_35 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
//const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n) - 1; i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r) - 1;i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define P pair<int,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
const int dx[4] = { 1,0,-1,0 };
const int dy[4] = { 0,-1,0,1 };
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int h, w; cin >> h >> w;
	vector a(h, vector<int>(w));
	rep(i, h)rep(j, w)cin >> a[i][j];
	vector col(h, vector<int>(w, -1));
	col[0][0] = 0;
	col[h - 1][w - 1] = 1;
	int cnt = 0;
	using T = pair<int, P>;
	vector< std::priority_queue<T, std::vector<T>, std::greater<T>>>pq(2);
	rep(i, 2) {
		rep(j, 4) {
			int x, y;
			if (0 == i) {
				x = 0 + dx[j];
				y = 0 + dy[j];
			}
			else {
				x = h - 1 + dx[j];
				y = w - 1 + dy[j];
			}
			if (x < 0 || x >= h)continue;
			if (y < 0 || y >= w)continue;
			pq[i].push({ a[x][y],{x,y} });
		}
	}
	while (true) {
		int t = cnt % 2;
		cnt++;
		while (true) {
			auto top = pq[t].top();
			if (-1 != col[top.second.first][top.second.second]) {
				pq[t].pop();
				continue;
			}
			break;
		}
		auto top = pq[t].top();
		pq[t].pop();
		col[top.second.first][top.second.second] = t;
		bool chk = false;
		rep(i, 4) {
			int x = top.second.first + dx[i];
			int y = top.second.second + dy[i];
			if (x < 0 || x >= h)continue;
			if (y < 0 || y >= w)continue;
			if (col[x][y] == 1 - t) {
				chk = true;
			}
			if (-1 != col[x][y])continue;
			pq[t].push({ a[x][y],{x,y} });
		}
		if (chk)break;
	}
	cout << cnt << endl;
	return 0;
}
0