poj1979 Red and Black

 1 #include<stdio.h>
 2 char m[20][20];
 3 int r,c,count;
 4 void cnt(int i,int j)//统计连续黑砖的块数 
 5 {
 6     if(m[i][j]=='#'||(i<0||j<0)||(i>r-1||j>c-1))//边界条件,除去 
 7         return;
 8     m[i][j]='#';//发现了一个新的黑砖,置'#',下次不在访问 
 9     count++;   //count+1
10     cnt(i,j-1);//往左寻找 
11     cnt(i-1,j);//往上寻找 
12     cnt(i,j+1);//往右寻找 
13     cnt(i+1,j);//往下寻找 
14 }
15 int main()
16 {
17     int i,j,x,y;
18     while(scanf("%d%d",&c,&r),r||c){
19         for(count=i=0;i<r;++i){
20             getchar();
21             for(j=0;j<c;++j){
22                 m[i][j]=getchar();
23                 if(m[i][j]=='@'){//查找起始点 
24                     x=i;
25                     y=j;
26                 }
27             }
28         }
29         cnt(x,y);
30         printf("%d\n",count);
31     }
32     return 0;
33 }
原文地址:https://www.cnblogs.com/shihuajie/p/2640597.html