結果

問題 No.307 最近色塗る問題多くない?
ユーザー __tatamo____tatamo__
提出日時 2016-03-01 22:40:14
言語 C++11
(gcc 11.4.0)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,426 bytes
コンパイル時間 620 ms
コンパイル使用メモリ 66,992 KB
実行使用メモリ 813,804 KB
最終ジャッジ日時 2023-10-24 19:24:01
合計ジャッジ時間 3,053 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 MLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<utility>

using namespace std;

typedef pair<int,int> ip;
#define Y first
#define X second

struct Query{
	int r;
	int c;
	int x;
};
class Island;

int h, w;
int **a;
Island*** islands;


class Island{
	private:
		void init(int y, int x){
			if(y<0 || x < 0 || y >= h || x >= w) return;
			for(int i=0; i < pos.size(); i++){
				if(pos[i].X == x && pos[i].Y == y){
					return;
				}
			}
			if(a[y][x] != color) {
				if(islands[y][x] != NULL){
					setJoinedIsland(islands[y][x]);
				}
				return;
			}
			pos.push_back(ip(y,x));
			islands[y][x] = this;
			init(y-1,x);
			init(y,x-1);
			init(y+1,x);
			init(y,x+1);
		}
	public:
		Island* parent;
		vector<Island*> childs;
		vector<Island*> joined_islands;
		vector<ip> pos;
		int color;
		int id;
		Island(int y,int x,int id){
			this->id = id;
			parent = NULL;
			color = a[y][x];
			init(y,x);
		}
		void setJoinedIsland(Island* island){
			for(int i=0; i<joined_islands.size(); i++){
				if(joined_islands[i] == island) return; 
			}
			joined_islands.push_back(island);
			island->setJoinedIsland(this);
		}
		void setParent(Island *p){
			parent = p;
			color = p->color;
			for(int i=0;i<childs.size();i++){
				childs[i]->setParent(p);
			}
		}
		void reverseColor(){
			color = (color+1)%2;
			for(int i=0;i<joined_islands.size();i++){
				joined_islands[i]->setParent(this);
				childs.push_back(joined_islands[i]);
			}
			joined_islands.clear();
		}
};


int main(){
	cin >> h >> w;
	a = new int*[h];
	islands = new Island**[h];
	for(int i=0;i<h;i++){
		a[i] = new int[w];
		islands[i] = new Island*[w];
	}
	for(int i=0;i<h;i++){
		for(int ii=0;ii<w;ii++){
			cin >> a[i][ii];
			islands[i][ii] = NULL;
		}
	}
	int q;
	cin >> q;
	Query* qa = new Query[q];
	for(int i=0; i<q; i++){
		cin >> qa[i].r >> qa[i].c >> qa[i].x;
		qa[i].r -= 1;
		qa[i].c -= 1;
	}

	int c=0;
	for(int i=0;i<h;i++){
		for(int ii=0;ii<w;ii++){
			if(islands[i][ii] == NULL){
				new Island(i,ii,c++);
			}
		}
	}

	for(int i=0;i<q;i++){
		Query query = qa[i];
		if(a[query.r][query.c] == query.x) continue; // same color
		islands[query.r][query.c]->reverseColor();
	}
	for(int i=0;i<h;i++){
		for(int ii=0;ii<w;ii++){
			cout << islands[i][ii]->color << " ";
			/*for(int iii=0;iii<islands[i][ii]->joined_islands.size();iii++){
				cout << islands[i][ii]->joined_islands[iii]->id << ", ";
			}
			cout << endl;*/
		}
		cout << endl;
	}
}
0