HDU 1241 Oil Deposits (DFS/BFS)

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15533    Accepted Submission(s): 8911


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
 
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <queue>
 4 using    namespace    std;
 5 
 6 const    int    SIZE = 105;
 7 char    MAP[SIZE][SIZE];
 8 int    UPDATE[][2] = {{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}};
 9 int    N,M;
10 int    ANS;
11 
12 struct    Node
13 {
14     int    x,y;
15 }QUE[SIZE * SIZE];
16 void    dfs(int x,int y);
17 void    bfs(int r,int c);
18 int    main(void)
19 {
20     while(scanf("%d%d",&N,&M) && (N || M))
21     {
22         ANS = 0;
23         for(int i = 1;i <= N;i ++)
24             for(int j = 1;j <= M;j ++)
25                 scanf(" %c",&MAP[i][j]);
26         for(int i = 1;i <= N;i ++)
27             for(int j = 1;j <= M;j ++)
28                 if(MAP[i][j] == '@')
29                 {
30                     ANS ++;
31                     bfs(i,j);
32                 }
33         printf("%d
",ANS);
34     }
35 
36     return    0;
37 }
38 
39 void    dfs(int x,int y)
40 {
41     MAP[x][y] = '*';
42     int    new_x,new_y;
43     for(int i = 0;i < 8;i ++)
44     {
45         new_x = x + UPDATE[i][0];
46         new_y = y + UPDATE[i][1];
47         if(new_x >= 1 && new_x <= N && new_y >= 1 && new_y <= M && MAP[new_x][new_y] == '@')
48         {
49             MAP[new_x][new_y] = '*';
50             dfs(new_x,new_y);
51         }
52     }
53 }
54 
55 void    bfs(int r,int c)
56 {
57     MAP[r][c] = '*';
58 
59     QUE[0].x = r;
60     QUE[0].y = c;
61     int    front,rear;
62     front = 0;
63     rear = 1;
64     
65     while(front < rear)
66     {
67         int    cur_x = QUE[front].x;
68         int    cur_y = QUE[front].y;
69         front ++;
70         
71         for(int i = 0;i < 8;i ++)
72         {
73             int    new_x = cur_x + UPDATE[i][0];
74             int    new_y = cur_y + UPDATE[i][1];
75             if(new_x >= 1 && new_x <= N && new_y >= 1 && new_y <= M && MAP[new_x][new_y] == '@')
76             {
77                 MAP[new_x][new_y] = '*';
78                 QUE[rear].x = new_x;
79                 QUE[rear].y = new_y;
80                 rear ++;
81             }
82         }
83     }
84 }
原文地址:https://www.cnblogs.com/xz816111/p/4402961.html