HDU_1241 Oil Deposits(DFS深搜)

Problem 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
題目大意:
        探測地下的石油儲量,含有油的地區被稱爲口袋。 若是兩個口袋是相鄰的,那麼它們是相同的石油礦牀的一部分。石油儲量可能很是大,可能含有大量的口袋。 你的任務是要肯定不一樣的石油儲量有多少包含在一個網格。
        輸入文件包含一個或多個網格。 每一個網格開始用含有m和n,行和列的網格中的號碼的線路,由一個空格分開。 若是m = 0它標誌着輸入的結束; 不然1 <= M <= 100,1 <= N <= 100。在此以後是每一個n字符(不包括端部的行的字符)m條。 每一個字符對應一個情節,'*',表明沒有油,'@',是有油。 
        對於每個網格,輸出不一樣油沉積物的數量。 一堆@連在一塊兒的算一個大油田(對角線,橫豎都算)。問有幾個大油田。
        
代碼以下:
 1 #include<stdio.h>
 2 #include<string.h>
 3 char map[110][110];
 4 int move[8][2]={1,0,-1,0,0,1,0,-1,1,1,-1,-1,1,-1,-1,1};//兩個座標一組 分爲8組
 5 int h,w;
 6 void dfs(int x,int y)//定義dfs函數,主函數找到了@,dfs啓動,尋找主函數找到的@八面存在的@
 7 {
 8     int next_x,next_y,i;
 9     map[x][y]='*';//把找到的@變爲*,以避免重複搜索
10     for(i=0;i<8;i++)
11     {
12         next_x=x+move[i][0];//[0]表示兩個座標一組的第一個[i]表示兩個座標一組的第幾組
13         next_y=y+move[i][1];//[1]表示兩個座標一組的第二個[i]表示兩個座標一組的第幾組
14         if(next_x>=0&&next_x<h&&next_y>=0&&next_y<w)
15         {
16             if(map[next_x][next_y]=='@')
17             {
18                 dfs(next_x,next_y);
19             }
20         }
21     }
22 }
23 int main()//主函數開始,尋找第一個@
24 {
25     int i,j,sum;
26     while(scanf("%d%d",&h,&w)&&(h!=0||w!=0))
27     {
28         for(i=0;i<h;i++)
29         scanf("%s",map[i]);
30         sum=0;
31         for(i=0;i<h;i++)
32         {
33             for(j=0;j<w;j++)
34             {
35                 if(map[i][j]=='@')
36                 {
37                     dfs(i,j);//轉移到dfs函數,由dfs函數開始搜索主函數找到@的八面存在的@
38                     sum++;
39                 }
40             }
41         }
42         printf("%d\n",sum);
43     }
44     return 0;
45 }
思路解析:
             經典DFS,大概思路寫進註釋裏面啦~
相關文章
相關標籤/搜索