1312 Red and Black

http://acm.hdu.edu.cn/showproblem.php?pid=1312

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 #include <stdio.h>
 5 int n,m;
 6 char data[30][30];
 7 using namespace std;
 8 int f(int i,int j)
 9 {
10     if(i<0||j>n-1||i>m-1||j<0)
11     return 0;
12     if(data[i][j]=='#')
13     return 0;
14     data[i][j]='#';
15     return 1+f(i-1,j)+f(i+1,j)+f(i,j-1)+f(i,j+1);
16 }
17 int main()
18 {
19     int x,y;
20     while(~scanf("%d%d",&n,&m)&&(n!=0||m!=0))
21     {
22         getchar();
23         for(int i=0;i<m;i++)
24         scanf("%s",data[i]);
25         for(int i=0;i<m;i++)
26         for(int j=0;j<n;j++)
27         if(data[i][j]=='@')
28         {
29             x=i;y=j;
30         }
31         cout<<f(x,y)<<endl;
32     }
33     return 0;
34 }
View Code
 1 #include<stdio.h>  
 2   
 3 char a[22][22];  
 4 int count,n,m;  
 5   
 6 void fab(int x,int y){  
 7     if(a[x][y]=='#')   
 8     return ;  
 9     if(x<1||x>m||y<1||y>n)  
10     return ;  
11     count++;  
12     a[x][y]='#';  
13     fab(x+1,y);  
14     fab(x-1,y);  
15     fab(x,y+1);  
16     fab(x,y-1);  
17 }  
18   
19 int main(){  
20     int x,y,i,j;  
21     while(scanf("%d%d",&n,&m),n|m){  
22         count=0;  
23         for(i=1;i<=m;i++){  
24             getchar();  
25             for(j=1;j<=n;j++){  
26                 scanf("%c",&a[i][j]);  
27                 if(a[i][j]=='@')  
28                 x=i,y=j;  
29             }  
30         }  
31         fab(x,y);  
32         printf("%d\n",count);  
33     }  
34     return 0;  
35 }  
View Code

这是第一个递归问题。

第一个是自己的代码,感觉还行吧!!!第二个是别人的代码。

posted @ 2016-03-23 23:35  Wally的博客  阅读(118)  评论(0编辑  收藏  举报