結果

問題 No.307 最近色塗る問題多くない?
ユーザー Yang33Yang33
提出日時 2018-09-30 13:32:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 142 ms / 4,000 ms
コード長 3,133 bytes
コンパイル時間 1,888 ms
コンパイル使用メモリ 172,292 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 13:33:23
合計ジャッジ時間 3,825 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 4 ms
5,376 KB
testcase_08 AC 16 ms
5,376 KB
testcase_09 AC 7 ms
5,376 KB
testcase_10 AC 3 ms
5,376 KB
testcase_11 AC 6 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 10 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 10 ms
5,376 KB
testcase_18 AC 25 ms
5,376 KB
testcase_19 AC 22 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 18 ms
5,376 KB
testcase_22 AC 15 ms
5,376 KB
testcase_23 AC 125 ms
5,376 KB
testcase_24 AC 48 ms
5,376 KB
testcase_25 AC 73 ms
5,376 KB
testcase_26 AC 141 ms
5,376 KB
testcase_27 AC 11 ms
5,376 KB
testcase_28 AC 18 ms
5,376 KB
testcase_29 AC 8 ms
5,376 KB
testcase_30 AC 142 ms
5,376 KB
testcase_31 AC 17 ms
5,376 KB
testcase_32 AC 15 ms
5,376 KB
testcase_33 AC 7 ms
5,376 KB
testcase_34 AC 15 ms
5,376 KB
testcase_35 AC 16 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/09/30  Problem: yukicoder 307  / Link: http://yukicoder.me/problems/no/307  ----- */
/* ------問題------

縦Hマス、横Wマスの方眼紙のマスを2色のインクで塗りつぶすことを考えます。

上から数えてR行目、左から数えてC列目のマスを(R,C)と表記します。 一番左上は(1,1)、一番右下は(H,W)です。
マス(R,C)ははじめ、色AR,Cで塗られています。

あるマス(R,C)と、それに隣接するマス(R′,C′)は、2つのマスが同色であるとき、 「(R,C)と(R′,C′)は連結である」とします。
また、(R,C)と(R′,C′)が連結で、かつ、(R′,C′)と(R′′,C′′)が連結であるとき、 (R,C)と(R′′,C′′)も連結であるとします。
ここで「隣接する」とは、(R,C)と(R′,C′)の マンハッタン距離が1 である場合、 即ち(R′,C′) が (R+1,C),(R−1,C),(R,C+1),(R,C−1)のいずれかである場合を指します。
「マス(R,C)と連結である全てのマスを 色X で塗りつぶす」というクエリがQ個与えられます。
Q回の塗りつぶしを終えた後の方眼紙の各マスの色を出力してください。

要するにペイントソフトの「塗りつぶし」機能です。

-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	int H, W; cin >> H >> W;
	VVI a(H, VI(W));
	FOR(i, 0, H) {
		FOR(j, 0, W) {
			cin >> a[i][j];
		}
	}
	int Q; cin >> Q;
	int f = 0;
	FOR(i, 0, Q) {
		int r, c, x; cin >> r >> c >> x;
		r--, c--;
		// comp(r,c) = x

		if (a[r][c] == x || (f&&i != Q - 1))continue;
		if (f && i == Q - 1) {
			a = VVI(H, VI(W, x)); break;
		}

		int cnt = 1;
		queue<PII>q;
		a[r][c] = x;
		q.push(PII(r, c));
		while (!q.empty()) {
			PII pos = q.front(); q.pop();
			FOR(k, 0, 4) {
				int ny = pos.first + DY[k], nx = pos.second + DX[k];
				if (0 <= ny && ny < H && 0 <= nx && nx < W) {
					if (a[ny][nx] != x) {
						a[ny][nx] = x;
						cnt++;
						q.push(PII(ny, nx));
					}
				}
			}
		}
		if (cnt == H * W)f = 1;
	}

	FOR(i, 0, H) {
		FOR(j, 0, W) {
			cout << a[i][j] << " \n"[j == W - 1];
		}
	}


	return 0;
}
0