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,大概思路寫進注釋里面啦~
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM