UVA572油田问题(DFS求连通块)

来源:互联网 发布:php汽车管理系统 编辑:程序博客网 时间:2024/05/12 19:59
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=110;
char ch[maxn][maxn];
int ind[maxn][maxn];
int m,n;
void caculate(int i,int j,int count){
if(i>=m||i<0||j>=n||j<0)return ;//出界的格子 
if(ind[i][j]||ch[i][j]!='@')return ;//不是@或者已经访问过的格子 
ind[i][j]=1;
{
for(int i1=-1;i1<=1;i1++){
for(int j1=-1;j1<=1;j1++){
if(i1!=0||j1!=0){
caculate(i+i1,j+j1,count);
}
}
}
}
}
int main(){
while(scanf("%d%d",&m,&n)==2&&m!=0&&n!=0){
memset(ind,0,sizeof(ind));
for(int i=0;i<m;i++){
scanf("%s",ch[i]);
//scanf("%c",&ch[i][j]);   //用这条语句输入时,空格符以及转义字符都作为有效字符输入 
}
int count=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(!ind[i][j]&&ch[i][j]=='@')caculate(i,j,++count);
}
}
printf("%d\n",count);
}
return 0;
}
0 0
原创粉丝点击