poj 1562 oil (Oil Deposits) DFS

Oil (Oil Deposits)
Topic Source:
Mid-Central USA 1997, ZOJ1709, POJ1562
Subject description:
GeoSurvComp geological survey of the company responsible for detecting underground oil fields. Every GeoSurvComp companies are in a Rectangle
Shaped land up detection field. In the detection, they put the land into several small squares with a grid, and then one by one analysis per
Piece of land, whether there are oil fields with detection equipment to detect underground. There is oil under the block of land called pocket, if two pocket
Adjacent, is considered to be the same piece of oilfield, oil field may cover multiple pocket. Your job is to calculate how much of the rectangular land
Different fields.
Enter a description:
The input data file contains a plurality of test, each test data describing a grid. The first line of each mesh data of two
Integers: mn, respectively, a grid of rows and columns; if m = 0, it indicates the end of input, or 1≤m≤100,1 ≤n
≤100. Then there are m rows of data, each row of data has n characters (not including the line terminator). Each character represents a small box,
If "*", it means that there is no oil, if it is "@", the representatives of the oil, is a pocket.
Output Description:
The number of fields for each grid in the input file, the output of a different mesh. If two different levels in the pocket, vertical
Adjacent straight or diagonal direction, it is considered part of the same oil field. Each pocket number field contains no more than
100。

Sample Input

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

Sample Output

0
1
2
2

 

This problem is relatively simple conventional DFS 
 
 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <algorithm>
 5 #include <memory.h>
 6 
 7 using namespace std;
 8 
 9 const int MAX_SIZE = 100;
10 
11 char g[MAX_SIZE][MAX_SIZE];
12 
13 int n, m;
14 int oilCount = 0;
15 
16 int addx[] = { -1,-1,-1,0,0,1,1,1};
17 int addy[] = { -1,0,1,-1,1,-1,0,1};
18 
19 
20 void DFS(int x,int y)
21 {
22     if (x < 0 || x >= n || y < 0 || y >= m) return;
23     if (g[x][y] != '@') return;
24     g[x][y] = '*';
25     for (int i = 0; i < 8; i++) {
26         int newx = x + addx[i];
27         int newy = y + addy[i];
28 
29         DFS(newx, newy);
30     }
31 }
32 
33 
34 int main()
35 {
36     while (1) {
37         cin >> n >> m;
38         if (n == m && m == 0) break;
39         memset(g, 0, sizeof g);
40         oilCount = 0;
41         for (int i = 0; i < n; i++) {
42             scanf("%s",g[i]);
43         }
44         
45         for (int i = 0; i < n; i++) {
46             for (int j = 0; j < m; j++) {
47                 if (g[i][j] == '@') {
48                     DFS(i, j);
49                     oilCount++;
50                 }
51             }
52         }
53 
54         cout << oilCount << endl;
55     }
56 
57 
58     return 0;
59 }
View Code

 

Guess you like

Origin www.cnblogs.com/itdef/p/12520175.html