結果

問題 No.2731 Two Colors
ユーザー AwashAmityOakAwashAmityOak
提出日時 2024-04-19 22:02:34
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 472 ms / 3,000 ms
コード長 3,043 bytes
コンパイル時間 6,860 ms
コンパイル使用メモリ 336,904 KB
実行使用メモリ 19,968 KB
最終ジャッジ日時 2024-04-19 22:02:54
合計ジャッジ時間 14,078 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 443 ms
18,944 KB
testcase_01 AC 457 ms
18,944 KB
testcase_02 AC 472 ms
18,944 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 21 ms
5,376 KB
testcase_05 AC 14 ms
5,376 KB
testcase_06 AC 61 ms
6,400 KB
testcase_07 AC 69 ms
7,192 KB
testcase_08 AC 11 ms
5,376 KB
testcase_09 AC 262 ms
15,824 KB
testcase_10 AC 6 ms
5,376 KB
testcase_11 AC 237 ms
19,532 KB
testcase_12 AC 261 ms
15,532 KB
testcase_13 AC 218 ms
14,496 KB
testcase_14 AC 116 ms
9,068 KB
testcase_15 AC 10 ms
5,376 KB
testcase_16 AC 94 ms
8,980 KB
testcase_17 AC 161 ms
10,900 KB
testcase_18 AC 27 ms
5,376 KB
testcase_19 AC 102 ms
9,004 KB
testcase_20 AC 168 ms
11,372 KB
testcase_21 AC 28 ms
5,376 KB
testcase_22 AC 264 ms
17,176 KB
testcase_23 AC 62 ms
6,716 KB
testcase_24 AC 33 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 218 ms
13,836 KB
testcase_27 AC 274 ms
17,412 KB
testcase_28 AC 166 ms
13,280 KB
testcase_29 AC 51 ms
6,068 KB
testcase_30 AC 94 ms
8,908 KB
testcase_31 AC 56 ms
7,532 KB
testcase_32 AC 344 ms
19,968 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
typedef long long ll;
typedef long double ld;
#define int ll
#define OVERLOAD_REP(_1, _2, _3, name, ...) name
#define REP2(i, l, r) for (int i = (int)(l); i < (int)(r); i++)
#define REP1(i, n) REP2(i, 0, n)
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define RREP2(i, l, r) for (int i = (int)(l)-1; i >= (int)(r); i--)
#define RREP1(i, n) RREP2(i, n, 0)
#define rrep(...) OVERLOAD_REP(__VA_ARGS__, RREP2, RREP1)(__VA_ARGS__)
#define all(...) begin(__VA_ARGS__), end(__VA_ARGS__)
#define rall(...) rbegin(__VA_ARGS__), rend(__VA_ARGS__)
#define debug(x) cerr << #x << ": " << (x) << endl
#define pb push_back
#define mp make_pair
#define mt make_tuple
const int INF = 4e18;
const int MOD = 1e9 + 7;
const int MOD2 = 998244353;
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }
template<class... T> void input(T&... args) { ((cin >> args), ...); }
template<class... T> void print(T... args) { ((cout << args << " "), ...); }
template<class... T> void println(T... args) { print(args...); cout << endl; }

signed main() {
	int H, W;
	input(H, W);
	vector<vector<int>> A(H, vector<int>(W));
	rep(i, H) rep(j, W) input(A[i][j]);
	vector<vector<int>> B(H, vector<int>(W, -1));
	B[0][0] = 1;
	B[H-1][W-1] = 0;
	
	priority_queue<
		pair<int, pair<int, int>>,
        vector<pair<int, pair<int, int>>>,
        greater<pair<int, pair<int, int>>>
    > color0_next;
    color0_next.push(mp(A[H-2][W-1], mp(H-2, W-1)));
    color0_next.push(mp(A[H-1][W-2], mp(H-1, W-2)));
    
	priority_queue<
		pair<int, pair<int, int>>,
        vector<pair<int, pair<int, int>>>,
        greater<pair<int, pair<int, int>>>
    > color1_next;
    color1_next.push(mp(A[1][0], mp(1, 0)));
    color1_next.push(mp(A[0][1], mp(0, 1)));
    
	int i = 1;
	while (true) {
		if (i % 2 == 0) {
			while (!color0_next.empty()) {
				auto [di, dj] = color0_next.top().second;
				color0_next.pop();
				if (B[di][dj] == -1) {
					B[di][dj] = 0;
					for (auto [ddi, ddj] : {mp(-1, 0), mp(0, -1), mp(1, 0), mp(0, 1)}) {
						ddi = max(0LL, min(H-1, di + ddi));
						ddj = max(0LL, min(W-1, dj + ddj));
						if (B[ddi][ddj] == 1) {
							println(i);
							return 0;
						} else if (B[ddi][ddj] == -1) {
							color0_next.push(mp(A[ddi][ddj], mp(ddi, ddj)));
						}
					}
					break;
				}
			}
		} else {
			while (!color1_next.empty()) {
				auto [di, dj] = color1_next.top().second;
				color1_next.pop();
				if (B[di][dj] == -1) {
					B[di][dj] = 1;
					for (auto [ddi, ddj] : {mp(-1, 0), mp(0, -1), mp(1, 0), mp(0, 1)}) {
						ddi = max(0LL, min(H-1, di + ddi));
						ddj = max(0LL, min(W-1, dj + ddj));
						if (B[ddi][ddj] == 0) {
							println(i);
							return 0;
						} else if (B[ddi][ddj] == -1) {
							color1_next.push(mp(A[ddi][ddj], mp(ddi, ddj)));
						}
					}
					break;
				}
			}
		}
		++i;
	}
}
0