Codeforces Round #771 (Div. 2) D

links

https://codeforces.com/contest/1638/problem/D

 

problem

there are n*m cells,each cell has a color.(color<=n*m)

everytime we can use a brush whose size is 2*2,if we choose (i,j) to paint,then (i,j),(i+1,j),(i,j+1),(i+1,j+1) will be in the same color(we can use any color we like,and new color can cover what we paint before)

given n*m cells and their colors,answer how to paint can we get this,or print -1 if it is impossible to make it

 

idea

consider last time when we use the brush,that must be 4 completely identical ones(show in 2*2 size),and we can start from that to solve the problem.

we start from here,and find the 2*2 squares around the initial square(maybe more than one ,maybe none)

by this method:

if we find a square that hasn't visit before,then we check whether the color in this square the same or not.

 have to mention that if we visit one 2*2 square,we mark every cell in it -1 in value,which means it has a particular color on the top,then the color below it can be any others

 

code(thanks to my classmates

void bfs(){
	while(!q.empty()){
		pair<int,int> now=q.front();
		q.pop();
		int color=-1;
		if(a[now.first][now.second]!=-1) color=a[now.first][now.second];
		else if(a[now.first+1][now.second]!=-1) color=a[now.first+1][now.second];
		else if(a[now.first][now.second+1]!=-1) color=a[now.first][now.second+1];
		else if(a[now.first+1][now.second+1]!=-1) color=a[now.first+1][now.second+1];
		if(color==-1) continue;
		ans.push({now.first,now.second,color});
		a[now.first][now.second]=-1;
		a[now.first+1][now.second]=-1;
		a[now.first][now.second+1]=-1;
		a[now.first+1][now.second+1]=-1;
		
		for(int dx=-1;dx<=1;dx++){
			for(int dy=-1;dy<=1;dy++){
				int nowx=dx+now.first;
				int nowy=dy+now.second;
				if(check(nowx,nowy)){
					set<int>st;
					if(a[nowx][nowy]!=-1) st.insert(a[nowx][nowy]);
					if(a[nowx+1][nowy]!=-1) st.insert(a[nowx+1][nowy]);
					if(a[nowx][nowy+1]!=-1) st.insert(a[nowx][nowy+1]);
					if(a[nowx+1][nowy+1]!=-1) st.insert(a[nowx+1][nowy+1]);
					if(st.size()==1) q.push(make_pair(nowx,nowy));
				}
			}
		}
		
	}
}

 

 

ps. my cin app is broken so i write this in english:(

posted @   starlightlmy  阅读(71)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示