今天李立教我们DFS,虽然以前就懂了那些思想,不过代码一直没有实践过,今天看了李立敲,终于 明白了DFS该怎么做,现在在研究BFS了……

下面介绍一下我的DFS吧。

首先,申请二维数组,输入那些数据,如果遇到要搜的,就进入DFS函数,然后进行深入的搜索,运用递归来实现,在递归之前,我们首先必须进行一个判断,看看是否搜索越界,如果没有越界,则进行进一步搜索(开始递归)。

主要的代码:

遇到第一个要搜索的东西的时候,进入DFS函数:

View Code
for( int i = 0; i < n; i++ )
{
for( int j = 0; j < m; j++ )
{
if( g[i][j] == '@' )
{
DFS( i, j );
sum++;
}
}
}

DFS的主要语句:

View Code
 1 void DFS( int y, int x )
2 {
3 g[y][x] = 1;
4 int xx, yy;
5 for( int i = 0; i < 8; i++ )
6 {
7 xx = x+dx[i];
8 yy = y+dy[i];
9 if( judge( xx, yy ) == 0 )
10 continue;
11 else
12 {
13 if( g[yy][xx] == '@' )
14 DFS( yy, xx );
15 }
16 }
17 }

不要忘了判断越界问题哦~~

View Code
int judge( int x, int y )
{
if( x < 0 || x > m || y < 0|| y > n )
return 0;
else
return 1;
}

具体的,我的代码是根据题目来的,题目见下面:

Description

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
 

Input

The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
 

Output

For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
 

Sample Input

1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
 

Sample Output

0
1
2
2
 
具体代码如下:
View Code
 1 #include<stdio.h>
2 int n, m;
3 char g[200][200];
4 int dy[10] = {-1,-1,-1,0,0,1,1,1};
5 int dx[10] = {-1,0,1,-1,1,-1,0,1};
6 int judge( int x, int y )
7 {
8 if( x < 0 || x > m || y < 0|| y > n )
9 return 0;
10 else
11 return 1;
12 }
13
14 void DFS( int y, int x )
15 {
16 g[y][x] = 1;
17 int xx, yy;
18 for( int i = 0; i < 8; i++ )
19 {
20 xx = x+dx[i];
21 yy = y+dy[i];
22 if( judge( xx, yy ) == 0 )
23 continue;
24 else
25 {
26 if( g[yy][xx] == '@' )
27 DFS( yy, xx );
28 }
29 }
30 }
31 int main()
32 {
33
34 while( scanf( "%d%d", &n, &m ) &&n &&m )
35 {
36 int sum = 0;
37 for( int i = 0; i < n; i++ )
38 {
39 for( int j = 0; j < m; j++ )
40 g[i][j] = 0;
41 }
42 for( int i = 0; i < n; i++ )
43 {
44 scanf( "%s", g[i] );
45 }
46 for( int i = 0; i < n; i++ )
47 {
48 for( int j = 0; j < m; j++ )
49 {
50 if( g[i][j] == '@' )
51 {
52 DFS( i, j );
53 sum++;
54 }
55 }
56 }
57 printf( "%d\n", sum );
58 }
59 return 0;
60 }




posted on 2012-02-27 22:20  狸の舞  阅读(196)  评论(0编辑  收藏  举报