九度oj 题目1460:Oil Deposit
Posted Jason杰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了九度oj 题目1460:Oil Deposit相关的知识,希望对你有一定的参考价值。
- 题目描述:
-
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.
- 输入:
-
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.
- 输出:
-
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.
- 样例输入:
-
1 1 * 3 5 *@*@* **@** *@*@* 1 8 @@****@* 5 5 ****@ *@@*@ *@**@ @@@*@ @@**@ 0 0
- 样例输出:
-
0 1 2 2
此题用深度优先搜索来求解,找到每一个‘@‘,将其能遍历到的‘@‘均改成‘*‘
能搜索几次就有几块.1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <queue> 5 #include <algorithm> 6 using namespace std; 7 8 char map[102][102]; 9 int n, m; 10 int dir[][2] = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 }, { 1, 1 }, { 1, -1 }, { -1, 1 }, { -1, -1 } }; 11 12 void dfs(int x, int y) { 13 map[x][y] = ‘*‘; 14 for (int i = 0; i < 8; i++) { 15 int xt = x + dir[i][0]; 16 int yt = y + dir[i][1]; 17 if (xt >= 0 && xt < m && yt >= 0 && yt < n) { 18 if (map[xt][yt] == ‘@‘) { 19 dfs(xt, yt); 20 } 21 } 22 } 23 24 } 25 int main(int argc, char const *argv[]) 26 { 27 //freopen("input.txt", "r", stdin); 28 //freopen("output.txt", "w", stdout); 29 while (scanf("%d %d", &m, &n) != EOF && m != 0) { 30 for (int i = 0; i < m; i++) { 31 scanf("%s", map[i]); 32 } 33 int ans = 0; 34 for (int i = 0; i < m; i++) { 35 for (int j = 0; j < n; j++) { 36 if (map[i][j] == ‘@‘) { 37 ans++; 38 dfs(i, j); 39 } 40 } 41 } 42 printf("%d\n", ans); 43 } 44 return 0; 45 46 } 47 48
以上是关于九度oj 题目1460:Oil Deposit的主要内容,如果未能解决你的问题,请参考以下文章