結果

問題 No.2731 Two Colors
ユーザー otamay6otamay6
提出日時 2024-04-20 18:37:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 677 ms / 3,000 ms
コード長 1,724 bytes
コンパイル時間 1,059 ms
コンパイル使用メモリ 88,420 KB
実行使用メモリ 12,148 KB
最終ジャッジ日時 2024-04-20 18:38:09
合計ジャッジ時間 9,675 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 662 ms
11,520 KB
testcase_01 AC 677 ms
11,776 KB
testcase_02 AC 645 ms
11,776 KB
testcase_03 AC 5 ms
5,376 KB
testcase_04 AC 25 ms
5,376 KB
testcase_05 AC 17 ms
5,376 KB
testcase_06 AC 65 ms
5,376 KB
testcase_07 AC 77 ms
5,376 KB
testcase_08 AC 13 ms
5,376 KB
testcase_09 AC 300 ms
10,636 KB
testcase_10 AC 6 ms
5,376 KB
testcase_11 AC 286 ms
11,872 KB
testcase_12 AC 291 ms
10,660 KB
testcase_13 AC 238 ms
9,500 KB
testcase_14 AC 132 ms
6,912 KB
testcase_15 AC 11 ms
5,376 KB
testcase_16 AC 109 ms
6,232 KB
testcase_17 AC 160 ms
7,800 KB
testcase_18 AC 29 ms
5,376 KB
testcase_19 AC 117 ms
6,244 KB
testcase_20 AC 187 ms
8,644 KB
testcase_21 AC 30 ms
5,376 KB
testcase_22 AC 301 ms
10,784 KB
testcase_23 AC 71 ms
5,376 KB
testcase_24 AC 37 ms
5,376 KB
testcase_25 AC 4 ms
5,376 KB
testcase_26 AC 240 ms
9,028 KB
testcase_27 AC 302 ms
12,148 KB
testcase_28 AC 185 ms
9,112 KB
testcase_29 AC 57 ms
5,376 KB
testcase_30 AC 102 ms
6,952 KB
testcase_31 AC 66 ms
5,620 KB
testcase_32 AC 377 ms
11,740 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>

using color_t = int;
constexpr int NONE = 2;

struct pos_t {
	int r;
	int c;
	int value;
	friend bool operator<(pos_t l, pos_t r){
		return l.value < r.value;
	}
	friend bool operator>(pos_t l, pos_t r){
		return l.value > r.value;
	}
};


int main(){
	int h, w;
	std::cin>>h>>w;
	std::vector<std::vector<int>> a(h);
	for(int i = 0; i < h; i++){
		for(int j = 0; j < w; j++){
			int x;std::cin>>x;
			a[i].push_back(x);
		}
	}
	std::vector<std::vector<color_t>> colors(h, std::vector<color_t>(w, NONE));
	colors[0][0] = 1;
	colors[h-1][w-1] = 0;
	std::priority_queue<pos_t, std::vector<pos_t>, std::greater<pos_t>> que[2];
	auto valid = [&](int r, int c){
		return 0 <=r && r < h && 0 <= c && c < w;
	};
	auto neighbors = [&](int r, int c){
		pos_t direction[4] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
		std::vector<pos_t> res;
		for(auto d: direction){
			d.r += r;
			d.c += c;
			if(valid(d.r, d.c)){
				d.value = a[d.r][d.c];
				res.push_back(d);
			}
		}
		return res;
	};
	for(auto pos: neighbors(0,0)){
		que[1].push(pos);
	}
	for(auto pos: neighbors(h-1, w-1)){
		que[0].push(pos);
	}
	
	int ans = 0;
	while(true){
		int color = ++ans % 2;
		bool clear = false;
		while(que[color].size()){
			pos_t pos = que[color].top(); que[color].pop();
			if(colors[pos.r][pos.c] != NONE){
				continue;
			}
			// std::cerr << pos.r << " " << pos.c << std::endl;
			colors[pos.r][pos.c] = color;
			for(auto pos:neighbors(pos.r, pos.c)){
				if(colors[pos.r][pos.c] == (color ^ 1)){
					// std::cerr << colors[pos.r][pos.c] << std::endl;
					clear = true;
				}
				que[color].push(pos);
			}
			break;
		}
		if(clear){
			break;
		}
	}
	
	std::cout << ans << std::endl;
}
0