程序代码:
#include <stdio.h>
#include <stdlib.h>
#define MaxN 100
int a[MaxN][MaxN],b[MaxN][MaxN],n,m;
void init()
{
int i,j;
scanf("%d%d",&n,&m);
for (i=0; i<n; i++)
for (j=0; j<m; j++) scanf("%d",&a[i][j]);
}
int Mark()
{
int i,j,flag=0;
memset(b,0,sizeof(b));
for (i=0; i<n; i++)
for (j=0; j<m; j++)
if (a[i][j]!=-1)
{
if (a[i][j]==a[i][j+1] && a[i][j]==a[i][j+2] && j+2<m)
{
b[i][j]=1; b[i][j+1]=1; b[i][j+2]=1; flag=1;
}
if (a[i][j]==a[i+1][j] && a[i][j]==a[i+2][j] && i+2<n)
{
b[i][j]=1; b[i+1][j]=1; b[i+2][j]=1; flag=1;
}
}
return flag;
}
void doremove()
{
int i,j;
for (i=0; i<n; i++)
for (j=0; j<m; j++)
if (b[i][j]==1) a[i][j]=-1;
}
void down()
{
int i,j,k;
for (j=0; j<m; j++)
{
k=n-1;
for (i=n-1; i>=0; i--)
if (a[i][j]!=-1) a[k--][j]=a[i][j];
for (i=k; i>=0; i--) a[i][j]=-1;
}
}
void print()
{
int i,j;
for (i=0; i<n; i++)
{
for (j=0; j<m; j++) printf("%d ",a[i][j]);
printf("\n");
}
}
int main()
{
init();
do
{
if (!Mark()) break;
doremove();
down();
}
while (1);
print();
system("pause");
}