Uva--705 (DFS+判圈)

2014-07-06 00:50:24

题意&思路:给出一个奇葩的图,让你DFS找圈,输出圈的个数和最长圈的长度。

           1 0                       0 1

方法:将一个'\' 表示成 0 1 ,将'/' 表示成 1 0,然后就可以通过细节判断来DFS,这里比较坑的是判圈,我通过len(圈长)> 2 来判。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 using namespace std;
 5 int g[200][200],used[200][200],num,flag,tmax;
 6 int dir[4][2] = {{0,-1},{-1,0},{0,1},{1,0}};
 7 int w,h,cnt,Case = 0;
 8 
 9 void Dfs(int x,int y,int sx,int sy,int len){
10     if(used[x][y]){
11         if(x == sx && y == sy && len > 2){
12             tmax = max(tmax,len);
13             flag = 1;
14         }
15         return;
16     }
17     if(x < 0 || y < 0 || x >= h || y >= w || g[x][y])
18         return;
19     used[x][y] = 1;
20     ++len;
21     if((x + y) % 2){
22         if(!g[x - 1][y - 1]) Dfs(x - 1,y - 1,sx,sy,len); //左上
23         if(!g[x + 1][y + 1]) Dfs(x + 1,y + 1,sx,sy,len); //右下
24         if(!g[x - 1][y + 1] && !g[x - 1][y] && !g[x][y + 1])
25             Dfs(x - 1,y + 1,sx,sy,len); //右上
26         if(!g[x + 1][y - 1] && !g[x][y - 1] && !g[x + 1][y])
27             Dfs(x + 1,y - 1,sx,sy,len); //左下
28     }
29     else{
30         if(!g[x + 1][y - 1]) Dfs(x + 1,y - 1,sx,sy,len); //左下
31         if(!g[x - 1][y + 1]) Dfs(x - 1,y + 1,sx,sy,len); //右上
32         if(!g[x - 1][y - 1] && !g[x][y - 1] && !g[x - 1][y])
33             Dfs(x - 1,y - 1,sx,sy,len); //左上
34         if(!g[x + 1][y + 1] && !g[x][y + 1] && !g[x + 1][y])
35             Dfs(x + 1,y + 1,sx,sy,len); //右下
36     }
37     for(int i = 0; i < 4; ++i){ //上下左右,x,y中只有一个改变
38         int tx = x + dir[i][0];
39         int ty = y + dir[i][1];
40         Dfs(tx,ty,sx,sy,len);
41     }
42 }
43 int main(){
44     char str[100];
45     while(scanf("%d %d",&w,&h) == 2){
46         if(!w && !h) break;
47         for(int i = 0; i < h; ++i){
48             scanf("%s",str);
49             for(int j = 0; j < w; ++j){
50                 if(str[j] == '/'){
51                     g[2 * i][2 * j] = 0;
52                     g[2 * i][2 * j + 1] = 1;
53                     g[2 * i + 1][2 * j] = 1;
54                     g[2 * i + 1][2 * j + 1] = 0;
55                 }
56                 else{
57                     g[2 * i][2 * j] = 1;
58                     g[2 * i][2 * j + 1] = 0;
59                     g[2 * i + 1][2 * j] = 0;
60                     g[2 * i + 1][2 * j + 1] = 1;
61                 }
62             }
63         }
64         h *= 2;
65         w *= 2;
66         tmax = 0;
67         cnt = 0;
68         memset(used,0,sizeof(used));
69         for(int i = 0; i < h; ++i){
70             for(int j = 0; j < w; ++j){
71                 if(!used[i][j] && g[i][j] == 0){
72                     num = 0;
73                     flag = 0;
74                     Dfs(i,j,i,j,0);
75                     if(flag) ++cnt;
76                 }
77             }
78         }
79         printf("Maze #%d:\n",++Case);
80         if(cnt)    printf("%d Cycles; the longest has length %d.\n\n",cnt,tmax);
81         else    printf("There are no cycles.\n\n");
82     }
83     return 0;
84 }

 

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3826632.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值