(DFS)hdoj1312-Red and Black

题目链接

非常简单的DFS,初学DFS做这道题很合适。需要注意的是题目中输入的行和列顺序是颠倒的。

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 int m,n,cnt,dir[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
 5 char a[22][22];
 6 void dfs(int si,int sj)
 7 {
 8     a[si][sj]='#';
 9     cnt++;
10     int sx,sy;
11     for(int i=0;i<4;i++)
12     {
13         sx=si+dir[i][0];
14         sy=sj+dir[i][1];
15         if(sx<0||sx>=m||sy<0||sy>=n||a[sx][sy]=='#')
16             continue;
17         else
18             dfs(sx,sy);
19     }
20 }
21 int main()
22 {
23     while(scanf("%d%d",&n,&m))
24     {
25         if(m==0&&n==0)
26             break;
27         int si,sj,i,j;
28         cnt=0;
29         for(i=0;i<m;i++)
30             scanf("%s",a[i]);
31         for(i=0;i<m;i++)
32             for(j=0;j<n;j++)
33                 if(a[i][j]=='@')
34         {
35             si=i;sj=j;
36         }
37         dfs(si,sj);
38         printf("%d\n",cnt);
39     }
40     return 0;
41 }

 

转载于:https://www.cnblogs.com/quintessence/p/6032430.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值