dfs题,这题我写出了一个模版,以后碰到相似的直接套程序就行了
逻辑简单,话不多说,上程序
#include<stdio.h>
#include<string.h>
char oil[105][105];
int visit[105][105];
int towards[8][2]={
{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1}};
int m,n;void dfs(int a,int b)
{int i,j;for(i=0;i<8;i++){int tmp1=a,tmp2=b;tmp1+=towards[i][0];tmp2+=towards[i][1];if(tmp1>=0&&tmp2>=0&&tmp1<m&&tmp2<n)if(oil[tmp1][tmp2]=='@'&&!visit[tmp1][tmp2]){visit[tmp1][tmp2]=1;dfs(tmp1,tmp2);}}
}
int main()
{while(scanf("%d%d",&m,&n)){if(m==0)break;int i,j,count=0;getchar();for(i=0;i<m;i++){for(j=0;j<n;j++)scanf("%c",&oil[i][j]);getchar();}memset(visit,0,sizeof(visit));for(i=0;i<m;i++)for(j=0;j<n;j++){if(oil[i][j]=='@'&&!visit[i][j]){visit[i][j]=1;dfs(i,j);count++;}}printf("%d\n",count);}return 0;
}