搜索第一题,直接模拟搜索就可以了
dx和dy分别表示上下左右四个方向的搜索画个坐标轴就可以理解
然后判断是否为*或者超出边界,搜索完之后记得标记为*表示已搜索过
递归求解
#include<stdio.h>
#include<string.h>
#define N 111
char str[N][N];
int a,b;
int dx[8]= {1,1,0,-1,-1,-1,0,1};
int dy[8]= {0,1,1,1,0,-1,-1,-1};
void bfs(int x,int y)
{
int i,n,m;
for(i=0; i<8; i++)
{
n=x+dx[i];
m=y+dy[i];
if(n<0||m<0||n>a-1||m>b-1||str[n][m]=='*')
continue;
str[n][m]='*';
bfs(n,m);
}
}
int main()
{
int i,j,count;
while(scanf("%d%d",&a,&b),a||b)
{
count=0;
for(i=0; i<a; i++)
scanf("%s",str[i]);
for(i=0; i<a; i++)
{
for(j=0; j<b; j++)
{
if(str[i][j]=='@')
{
str[i][j]='*';
bfs(i,j);
count++;
}
}
}
printf("%d\n",count);
}
return 0;
}