結果

問題 No.307 最近色塗る問題多くない?
ユーザー __tatamo____tatamo__
提出日時 2016-03-02 00:27:30
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,911 bytes
コンパイル時間 755 ms
コンパイル使用メモリ 67,700 KB
実行使用メモリ 12,648 KB
最終ジャッジ日時 2023-10-24 19:27:54
合計ジャッジ時間 14,544 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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:
		Island* parent;
		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:
		vector<Island*> childs;
		vector<Island*> joined_islands;
		vector<ip> pos;
		int id;
		int color;
		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){
			getParent()->parent = p;
		}
		Island* getParent(){
			if(parent == NULL) return this;
			return parent->getParent();
		}
		int getColor(){
			return getParent()->color;
		}
		void reverseColor(){
			if(parent!=NULL) {
				getParent()->reverseColor();
				return;
			}
			color = (color+1)%2;
			merge();
		}
		void merge(){
			if(joined_islands.size()>0){
				Island* p = getParent();
				for(int i=0;i<joined_islands.size();i++){
					Island* target = joined_islands[i]->getParent();
					if(target == p) continue;
					bool flg = true;
					for(int ii=0;ii<p->childs.size();ii++){
						if(p->childs[ii] == target) flg = false;
					}
					if(flg) {
						p->childs.push_back(target);
						target->setParent(p);
					}
				}
				joined_islands.clear();
			}
			else{
				vector<Island*> tmp = childs;
				childs.clear();
				for(int i=0;i<tmp.size();i++){
					tmp[i]->merge();
				}
			}
		}
};


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++){
		cout << "query " << i << endl;
		Query query = qa[i];
		if(islands[query.r][query.c]->getColor() == 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]->getColor() << " ";
		}
		cout << endl;
	}

}
0