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

这是第一个递归问题。

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

转载于:https://www.cnblogs.com/wang-ya-wei/p/5313723.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值