結果

問題 No.307 最近色塗る問題多くない?
ユーザー koyumeishikoyumeishi
提出日時 2015-09-21 23:09:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 173 ms / 4,000 ms
コード長 1,478 bytes
コンパイル時間 659 ms
コンパイル使用メモリ 81,104 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-14 03:40:07
合計ジャッジ時間 4,191 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 7 ms
4,380 KB
testcase_08 AC 26 ms
4,380 KB
testcase_09 AC 11 ms
4,376 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 10 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 21 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 12 ms
4,376 KB
testcase_18 AC 31 ms
4,380 KB
testcase_19 AC 28 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 24 ms
4,376 KB
testcase_22 AC 22 ms
4,380 KB
testcase_23 AC 138 ms
4,376 KB
testcase_24 AC 57 ms
4,376 KB
testcase_25 AC 97 ms
4,376 KB
testcase_26 AC 173 ms
4,376 KB
testcase_27 AC 23 ms
4,380 KB
testcase_28 AC 37 ms
4,380 KB
testcase_29 AC 15 ms
4,380 KB
testcase_30 AC 172 ms
4,376 KB
testcase_31 AC 37 ms
4,380 KB
testcase_32 AC 35 ms
4,376 KB
testcase_33 AC 12 ms
4,380 KB
testcase_34 AC 36 ms
4,376 KB
testcase_35 AC 36 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

int dx[] = {0,0,1,-1,0,0};
int* dy = dx+2;

int bfs(vector<vector<int>>& A, int r, int c, int x){
	int h = A.size();
	int w = A[0].size();

	int tmp = A[r][c];

	int ret = 1;

	queue<int> q;
	q.push(r*w+c);
	A[r][c] = x;

	while(q.size()){
		int pos = q.front();
		q.pop();
		r = pos/w;
		c = pos%w;
		for(int k=0; k<4; k++){
			int r_ = r + dy[k];
			int c_ = c + dx[k];
			if(r_<0 || r_>=h || c_<0 || c_>=w) continue;
			if(A[r_][c_] != tmp) continue;
			A[r_][c_] = x;
			ret++;
			q.push(r_*w + c_);
		}
	}

	return ret;
}


#include <ctime>

int main(){
	auto start = clock();
	int h,w;
	cin >> h >> w;
	vector<vector<int>> A(h, vector<int>(w));
	
	for(int i=0; i<h; i++){
		for(int j=0; j<w; j++){
			cin >> A[i][j];
		}
	}

	bool filled = false;
	int color = -1;

	int q;
	cin >> q;
	for(int i=0; i<q; i++){
		int r,c,x;
		cin >> r >> c >> x;
		r--; c--;

		color = x;

		if(A[r][c] == x) continue;
		if(filled || bfs(A, r,c, x) == h*w){
			filled = true;
		}
	}

	if(filled){
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				printf("%d%s", color, (j==w-1)?"\n":" ");
			}
		}
	}else{
		for(int i=0; i<h; i++){
			for(int j=0; j<w; j++){
				printf("%d%s", A[i][j], (j==w-1)?"\n":" ");
			}
		}
	}

	auto end = clock();
	//cerr << end-start << endl;

	return 0;
}
0