问题分析:另开一个数组,标记需要消除的颜色。
代码如下
#include<cstdio>
using namespace std;
const int N = 35;
int arr[N][N];
bool clear[N][N] = {false};
int main()
{
int n, m;
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < m; j ++)
{
scanf("%d", &arr[i][j]);
}
}
for(int i = 0; i < n; i ++)//行消除
{
for(int j = 1; j < m-1; j ++)
{
if(arr[i][j] == arr[i][j-1] && arr[i][j] == arr[i][j+1])
clear[i][j] = clear[i][j-1] = clear[i][j+1] = true;
}
}
for(int j = 0; j < m; j ++)
{
for(int i = 1; i < n-1; i ++)
{
if(arr[i][j] == arr[i-1][j] && arr[i][j] == arr[i+1][j])
clear[i][j] = clear[i-1][j] = clear[i+1][j] = true;
}
}
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < m; j ++)
{
if(clear[i][j]) arr[i][j] = 0;
}
}
//输出
for(int i = 0; i < n; i ++)
{
for(int j = 0; j < m-1; j ++)
{
printf("%d ", arr[i][j]);
}
printf("%d\n", arr[i][m-1]);
}
return 0;
}