D. Big Brush

Big Brush
题意:每次涂色2×2的矩阵 (可以覆盖),给出目标矩阵,求涂色方案。
思路:反过来想,最后一次涂色一定 是2×2的矩阵内颜色相同(可以多个起点),反过来bfs
修改已经处理过的矩阵为0;

//#pragma GCC optimize(2)
//#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int inf=8e18;
const int maxn=2e5+100;
int a[1100][1100],n,m;
bool vis[1100][1100];
int dir[4][2]= {0,0,1,0,0,1,1,1};
int dir2[8][2]= {-1,-1  ,-1,0  ,-1,1  ,0,-1  ,1,-1  ,0,1  ,1,1  ,1,0};
void out()
{
	cout<<"\n";
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=m;j++)
		{
			cout<<a[i][j]<<" ";
		}
		cout<<"\n";
	}
	cout<<"\n";
}
int ck(int x,int y)
{
	//bool fg=1;
	if(x<=0||x>=n||y<=0||y>=m)return 0;
	int num=0;
	for(int i=0; i<4; i++)
	{
		int x1=x+dir[i][0];
		int y1=y+dir[i][1];
		if(x1<=0||x1>n||y1<=0||y1>m)continue;
		if(a[x1][y1]!=0)num=a[x1][y1];
	}
	for(int i=0; i<4; i++)
	{
		int x1=x+dir[i][0];
		int y1=y+dir[i][1];
		if(x1<=0||x1>n||y1<=0||y1>m)continue;
		if(a[x1][y1]!=0&&a[x1][y1]!=num)return 0;
	}
	return num;
}
void gui(int x,int y)
{
	for(int i=0; i<4; i++)
	{
		int x1=x+dir[i][0];
		int y1=y+dir[i][1];
		if(x1<=0||x1>n||y1<=0||y1>m)continue;
		a[x1][y1]=0;
	}
}
int x,y;
stack<tuple<int,int,int>>s;
void sol(int x,int y)
{
	queue<pii>q;
	q.push({x,y});
	vis[x][y]=1;
	//s.push({x,y,ck(x,y)});
	while(!q.empty())
	{
		pii now=q.front();
		q.pop();
		int num=ck(now.fi,now.se);
		vis[now.fi][now.se]=0;
		if(num==0)continue;
		s.push({now.fi,now.se,num});
		gui(now.fi,now.se);
		for(int i=0; i<8; i++)
		{
			int x1=now.fi+dir2[i][0];
			int y1=now.se+dir2[i][1];
			if(x1<=0||x1>=n||y1<=0||y1>=m)continue;
			if(vis[x1][y1]==0){q.push({x1,y1});vis[x1][y1]=1;}
		}
		//out();
	}
}
signed main()
{
	IOS
	cin>>n>>m;
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++)
			cin>>a[i][j];
	vector<pii>v;		
	for(int i=1; i<n; i++)
	{
		for(int j=1; j<m; j++)
		{
			if(ck(i,j))
			{
				x=i;
				y=j;
				v.pb({i,j});
			}
		}
	}
	if(x==0&&y==0)
	{
		cout<<"-1\n";
		return 0;
	}
	for(auto it:v) sol(it.fi,it.se);
	//sol();
	//bool fg=1;
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=m; j++)
		{
			if(a[i][j]!=0)
			{
				cout<<"-1\n";
				return 0;
			}
		}
	}
	cout<<s.size()<<"\n";
	while(!s.empty())
	{
		auto now=s.top();
		s.pop();
		cout<<get<0>(now)<<" "<<get<1>(now)<<" "<<get<2>(now)<<"\n";
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

thusloop

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值