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:(
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」