DFS基础题

hdu 1241 油田  裸DFS

题意:@代表油田 8个方向上还有@就相连 相当于求图中连通子图的个数
Sample Input
1 1 // n m
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0

Sample Output
0
1
2
2

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 int d[8][2] = {{1,0},{1,-1},{1,1},{0,-1},{0,1},{-1,0},{-1,-1},{-1,1}};
 9 char map[110][110] ;
10 
11 int n , m ;
12 
13 void dfs(int x , int y)
14 {
15     int fx , fy ;
16     int i ;
17     map[x][y] = '*' ;
18     for (i = 0 ; i < 8 ; i++)
19     {
20         fx = x + d[i][0] ;
21         fy = y + d[i][1] ;
22         if (fx>=0 && fx<n && fy>=0 && fy<m && map[fx][fy] == '@')
23         {
24             dfs(fx,fy) ;
25         }
26     }
27 }
28 
29 int main()
30 {
31     //freopen("in.txt","r",stdin) ;
32 
33 
34    while (scanf("%d %d" , &n , &m) !=EOF)
35    {
36        if (n == 0 && m == 0)
37           break ;
38        int i , j ;
39 
40 
41        for (i = 0 ; i < n ; i++)
42        {
43            for (j = 0 ; j < m ; j++)
44           {
45               cin>>map[i][j];         //用scanf一直WA=.=
46           }
47 
48        }
49        int sum = 0 ;
50        for (i = 0 ; i < n ; i++)
51         for (j = 0 ; j < m ; j++)
52        {
53            if (map[i][j] == '@')
54            {
55                sum++ ;
56 
57                dfs(i,j) ;
58            }
59        }
60        printf("%d\n" , sum) ;
61 
62    }
63 
64     return 0;
65 }
View Code

 

hdu 2952 

上下左右 4个方向相连 求连通子图的个数
Sample Input
2
4 4
#.#. //#能走
.#.#
#.##
.#.#
3 5
###.#
..#..
#.###

Sample Output
6
3

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 int d[4][2] = {{1,0},{0,-1},{0,1},{-1,0}};
 9 char map[110][110] ;
10 
11 int n , m ;
12 
13 void dfs(int x , int y)
14 {
15     int fx , fy ;
16     int i ;
17     map[x][y] = '.' ;
18     for (i = 0 ; i < 4 ; i++)
19     {
20         fx = x + d[i][0] ;
21         fy = y + d[i][1] ;
22         if (fx>=0 && fx<n && fy>=0 && fy<m && map[fx][fy] == '#')
23         {
24             dfs(fx,fy) ;
25         }
26     }
27 }
28 
29 int main()
30 {
31    // freopen("in.txt","r",stdin) ;
32    int T ;
33    scanf("%d" , &T) ;
34 
35    while (T--)
36    {
37        scanf("%d %d" , &n , &m) ;
38        if (n == 0 && m == 0)
39           break ;
40        int i , j ;
41 
42 
43        for (i = 0 ; i < n ; i++)
44        {
45            for (j = 0 ; j < m ; j++)
46           {
47               cin>>map[i][j];
48           }
49 
50        }
51        int sum = 0 ;
52        for (i = 0 ; i < n ; i++)
53         for (j = 0 ; j < m ; j++)
54        {
55            if (map[i][j] == '#')
56            {
57                sum++ ;
58                dfs(i,j) ;
59            }
60        }
61        printf("%d\n" , sum) ;
62 
63    }
64 
65     return 0;
66 }
View Code

 

 

hdu 1312 能走多少格  裸DFS

 

 

Sample Input
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.

11 9
.#......... //.能走 #不能走 @起点
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
0 0

 

Sample Output
45
59
6
13

 

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<algorithm>
 6 using namespace std;
 7 
 8 int d[4][2] = {{0,1},{0,-1},{1,0},{-1,0} } ;
 9 char map[110][110] ;
10 
11 int n , m ;
12 int sum = 0 ;
13 
14 void dfs(int x , int y)
15 {
16     int fx , fy ;
17     int i ;
18     sum++ ;
19     map[x][y] = '#';
20     for (i = 0 ; i < 4 ; i++)
21     {
22         fx = x + d[i][0] ;
23         fy = y + d[i][1] ;
24         if (fx>=0 && fx<n && fy>=0 && fy<m && map[fx][fy] == '.')
25         {
26             dfs(fx,fy) ;
27         }
28     }
29 }
30 
31 int main()
32 {
33    // freopen("in.txt","r",stdin) ;
34 
35 
36    while (scanf("%d %d" , &m , &n) !=EOF)
37    {
38        if (n == 0 && m == 0)
39           break ;
40        int i , j ;
41        int bx , by ;
42        sum = 0 ;
43 
44        for (i = 0 ; i < n ; i++)
45        {
46            for (j = 0 ; j < m ; j++)
47           {
48               cin>>map[i][j];
49           }
50 
51        }
52        for (i = 0 ; i < n ; i++)
53        {
54            for (j = 0 ; j < m ; j++)
55           {
56               if (map[i][j] == '@')
57               {
58                   bx = i ;
59                   by = j ;
60               }
61           }
62 
63        }
64 
65        dfs(bx,by) ;
66 
67        printf("%d\n" , sum) ;
68 
69    }
70 
71     return 0;
72 }
View Code

 

posted @ 2015-05-15 20:20  __Meng  阅读(433)  评论(0编辑  收藏  举报