北京理工大学电子信息谢锦峰的答案,只要不是很奇怪的输入就应该可以过。
然而北理网络教室的一个隐藏用例过不了。艹
#include<stdio.h>
struct lei
{int n,m;
char ch[110][110];
};
static struct lei work[100];
void output(int ii);
int jishu(int j,int k,int ii);
void main()
{ int pd1=1,pd2=1; int i=1,j,k;
int flag=1;
while(flag)
{scanf("%d%d",&pd1,&pd2);getchar();
//printf("%d,%d\n",pd1,pd2);
if(pd1==0||pd2==0) flag=0;
else
{work[i].n=pd1;
work[i].m=pd2;
for(j=1;j<=work[i].n;j++)
{for(k=1;k<=work[i].m;k++)
scanf("%c",&work[i].ch[j][k]);
getchar();
}
i++;
}
}
/*for(j=1;j<=work[i].n;j++)
{for(k=1;k<=work[i].m;k++)
printf("%c,",&work[i].ch[j][k]);
printf("\n");
}*/
int ii;
for(ii=1;ii<=i-1;ii++)
{if(ii==1)printf("Field #%d:\n",ii);
else printf("\nField #%d:\n",ii);
output(ii);
//if(ii==1&&i!=1)printf("\n");
}
}
void output(int ii)
{int j,k;
for(j=1;j<=work[ii].n;j++)
{for(k=1;k<=work[ii].m;k++)
{if(work[ii].ch[j][k]=='*')
printf("*");
else
{printf("%d",jishu(j,k,ii));
}
}
printf("\n");
}
}
int jishu(int j,int k,int ii)
{int ji,ki,count=0;
if(j==1&&k==1)
{if(work[ii].ch[1][2]=='*')count++;
if(work[ii].ch[2][1]=='*')count++;
if(work[ii].ch[2][2]=='*')count++;
return count;
}
if(j==1&&k==work[ii].m)
{if(work[ii].ch[1][k-1]=='*')count++;
if(work[ii].ch[2][k-1]=='*')count++;
if(work[ii].ch[2][k]=='*')count++;
return count;
}
if(j==work[ii].n&&k==1)
{if(work[ii].ch[j-1][1]=='*')count++;
if(work[ii].ch[j-1][2]=='*')count++;
if(work[ii].ch[j][2]=='*')count++;
return count;
}
if(j==work[ii].n&&k==work[ii].m)
{if(work[ii].ch[j-1][k]=='*')count++;
if(work[ii].ch[j][k-1]=='*')count++;
if(work[ii].ch[j][k]=='*')count++;
return count;
}
//四个角落处理完毕
if(j==1&&k>1&&k<work[ii].m)
{for(ji=1;ji<=2;ji++)
for(ki=k-1;ki<=k+1;ki++)
{if(k==ki&&j==ji)continue;
if(work[ii].ch[ji][ki]=='*')
count++;
}
return count;
}
if(j==work[ii].n&&k>1&&k<work[ii].m)
{for(ji=j-1;ji<=j;ji++)
for(ki=k-1;ki<=k+1;ki++)
{if(k==ki&&j==ji)continue;
if(work[ii].ch[ji][ki]=='*')
count++;
}
return count;
}
if(j>1&&j<work[ii].n&&k==1)
{for(ji=j-1;ji<=j+1;ji++)
for(ki=1;ki<=2;ki++)
{if(k==ki&&j==ji)continue;
if(work[ii].ch[ji][ki]=='*')
count++;
}
return count;
}
if(j>1&&j<work[ii].n&&k==work[ii].m)
{for(ji=j-1;ji<=j+1;ji++)
for(ki=k-1;ki<=k;ki++)
{if(k==ki&&j==ji)continue;
if(work[ii].ch[ji][ki]=='*')
count++;
}
return count;
}
//四条边处理完毕
if(j>1&&j<work[ii].n&&k>1&&k<work[ii].m)
{for(ji=j-1;ji<=j+1;ji++)
for(ki=k-1;ki<=k+1;ki++)
{if(k==ki&&j==ji)continue;
if(work[ii].ch[ji][ki]=='*')
count++;
}
return count;
}
}
温馨提示:答案为网友推荐,仅供参考