外包接到想放弃学习…………
思路的话就那数组标记下哪些是可以消除的,不能直接修改原数组,因为可能会阶段另一个方向的消除。
消除的时候就横着扫一遍,竖着扫一遍
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define maxn 33
int a[maxn][maxn],del[maxn][maxn];
int main(){
int n,m;
cin>>n>>m;
memset(del,0,sizeof(del));
for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin>>a[i][j];
for(int i=0,j=0;i<n;i++)
{
int pre=a[i][0],tmp=1,st=0;
for(j=1;j<m;j++)
{
if(a[i][j]==pre) tmp++;
else
{
if(tmp>=3)
{
for(int k=st;k<j;k++) del[i][k]=1;
}
tmp=1;pre=a[i][j];st=j;
}
}
if(tmp>=3) for(int k=st;k<j;k++) del[i][k]=1;
}
for(int j=0,i=0;j<m;j++)
{
int pre=a[0][j],tmp=1,st=0;
for(i=1;i<n;i++)
{
if(a[i][j]==pre) tmp++;
else
{
if(tmp>=3)
{
for(int k=st;k<i;k++) del[k][j]=1;
}
tmp=1;pre=a[i][j];st=i;
}
}
if(tmp>=3)
{
for(int k=st;k<i;k++) del[k][j]=1;
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(del[i][j]) cout<<0<<" ";
else cout<<a[i][j]<<" ";
}
cout<<endl;
}
return 0;
}