这道题主要注意输入的问题,以及对周围搜索时的注意,要使用递归,多次调用,附上一组数据
11 20
*@*@*@@@**@*@**@@@**
***@*@*@*@*@****@**@
*@*@*@*@*@*@********
@*************@@**@*
@*@*@*@*@***********
*@*@*@*@*@*@*@*@****
**********@*@*@*****
******@*@*@*@*@*@***
**@*@***************
***********@*@*@*@**
*@*@*@******@*******
20
#include<stdio.h>
char xx[][];
int dps(int i,int j)
{
if(xx[i-][j-]=='@')
{
xx[i-][j-]='#';
dps(i-,j-);
}
if(xx[i-][j]=='@')
{
xx[i-][j]='#';
dps(i-,j);
}
if(xx[i-][j+]=='@')
{
xx[i-][j+]='#';
dps(i-,j+);
}
if(xx[i][j-]=='@')
{
xx[i][j-]='#';
dps(i,j-);
}
if(xx[i][j+]=='@')
{
xx[i][j+]='#';
dps(i,j+);
}
if(xx[i+][j-]=='@')
{
xx[i+][j-]='#';
dps(i+,j-);
}
if(xx[i+][j]=='@')
{
xx[i+][j]='#';
dps(i+,j);
}
if(xx[i+][j+]=='@')
{
xx[i+][j+]='#';
dps(i+,j+);
}
}
int main()
{
int m,n,i,j,ans;
while()
{
scanf("%d%d",&m,&n);
if(m==) break;
for(i=;i<=;i++)
for(j=;j<=;j++)
xx[i][j]='*';
for(i=;i<=m;i++)
scanf("%s",xx[i]);
ans=;
for(i=;i<=m;i++)
for(j=;j<=n;j++)
{
if(xx[i][j]=='@')
{
xx[i][j]='#';
ans++;
dps(i,j);
}
/*else if(xx[i-1][j-1]!='#'&&xx[i-1][j]!='#'&&xx[i-1][j+1]!='#'&&xx[i][j-1]!='#'&&xx[i][j+1]!='#'&&xx[i+1][j-1]!='#'&&xx[i+1][j]!='#'&&xx[i+1][j+1]!='#')
{
ans++;
if(xx[i-1][j-1]=='@') xx[i-1][j-1]='#';
if(xx[i-1][j]=='@') xx[i-1][j]='#';
if(xx[i-1][j+1]=='@') xx[i-1][j+1]='#';
if(xx[i][j-1]=='@') xx[i][j-1]='#';
if(xx[i][j+1]=='@') xx[i][j+1]='#';
if(xx[i+1][j-1]=='@') xx[i+1][j-1]='#';
if(xx[i+1][j]=='@') xx[i+1][j]='#';
if(xx[i+1][j+1]=='@') xx[i+1][j+1]='#';
}
}*/ }
printf("%d\n",ans);
/* for(i=1;i<=m;i++)
{
for(j=0;j<=n;j++)
printf("%c",xx[i][j]);
printf("\n");
}*/
}
return ;
}