課程練習二-1011-Oil Deposits

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. <br>
 

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.<br>
 

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.<br>
 

Sample Input
1 1<br>*<br>3 5<br>*@*@*<br>**@**<br>*@*@*<br>1 8<br>@@****@*<br>5 5 <br>****@<br>*@@*@<br>*@**@<br>@@@*@<br>@@**@<br>0 0 <br>
 

Sample Output

0<br>1<br>2<br>2<br>

題意:就是一個二維字符數組,裏面只有* 或 @,向上、下、左、右、和對角 八個方向檢索,判斷是否有@,若不能夠連續的

檢索到@,則繼續。求最多有多少組。

題目中An oil deposit will not contain more than 100 pockets.這句話,剛開始我以爲是超過100個@就多計數一次呢。

思路:dfs

代碼:

#include<iostream> #include<stdlib.h> #include<string.h> using namespace std; int ans = 0; int cou = 0; char point[102][102]; bool flag[102][102]; int m=0, n=0; void bfs(int a,int b) { /*if (cou > 100) { cou = 0; ans++; }*/ if (point[a][b] == '@'&&!flag[a][b]) { flag[a][b] = true; cou++; } else return; if (a - 1 > 0) { if (point[a - 1][b] == '@') { bfs(a - 1, b); } } if (a+ 1 < m) { if (point[a + 1][b] == '@') { bfs(a + 1, b); } } if (b - 1 > 0) { if (point[a][b-1] == '@') { bfs(a, b-1); } } if (b + 1 < n) { if (point[a][b+1] == '@') { bfs(a, b+1); } } if (a - 1 > 0&&b-1>0) { if (point[a - 1][b-1] == '@') { bfs(a - 1, b-1); } } if (a + 1 < m&&b+1<n) { if (point[a + 1][b+1] == '@') { bfs(a + 1, b+1); } } if (a - 1 > 0 && b + 1>0) { if (point[a - 1][b + 1] == '@') { bfs(a - 1, b + 1); } } if (a + 1 < m&&b - 1<n) { if (point[a + 1][b - 1] == '@') { bfs(a + 1, b - 1); } } } int main() { while (1) { int m1, n1; cin >> m1 >> n1; m = m1; n = n1; if (m == 0 && n == 0) break; ans = 0; cou = 0; /* for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) point[i][j]='0'; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) flag[i][j] = false; */ memset(point, 0, sizeof(point)); memset(flag, 0, sizeof(flag)); /* for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) cout << flag[i][j]; */ for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) cin >> point[i][j]; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) { if (point[i][j] == '@'&&flag[i][j]==false) { ans++; bfs(i, j); } } cout << ans << endl; } system("pause"); return 0; }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章