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";
}
}