您的位置:首页 > 其它

Program2_1011

2016-04-15 21:17 232 查看
我现在做的是第二专题编号为1011的试题,试题内容如下所示:

Oil Deposits

Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 55 Accepted Submission(s) : 21
[align=left]Problem Description[/align]
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>

[align=left]Input[/align]
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>

[align=left]Output[/align]
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>

[align=left]Sample Input[/align]

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>


[align=left]Sample Output[/align]

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


简单题意:

探索油田的数量,用@表示存在油田,*表示不存在油田,如果两个油田相邻,则将这两个看成一个油田,计算油田的数量

解题思路:

实际上这几个题的本质都是利用深度优先搜索进行计算油田的数量,建立一个 标记数组,再建立一个数组进行保存油田信息,然后建立一定的规则对数组的每一个数组进行搜索,看是否满足要求,对已经访问过的位置进行标记,以免重复搜索。

编写代码:

#include <iostream>

#include <cstdio>

#include <cstring>

using namespace std;

const int maxn = 200;

char dep[maxn][maxn];

int id[maxn][maxn];

int row, col;

void dfs(int r, int c, int num) {

if(r >= row || r < 0 || c >= col || c < 0) return;

if(dep[r][c] != '@' || id[r][c] != 0) return;

id[r][c] = num;

for(int i = -1; i <= 1; i++)

for(int j = -1; j <= 1; j++)

if(i != 0 || j != 0) dfs(r + i, c + j, num);

}

int main()

{

while(scanf("%d%d", &row, &col) == 2 && row && col) {

for(int i = 0; i < row; i++)

scanf("%s", dep[i]);

int num = 0;

memset(id, 0, sizeof(id));

for(int i = 0; i < row; i++)

for(int j = 0; j < col; j ++) {

if(id[i][j] == 0 && dep[i][j] == '@') dfs(i, j, ++num);

}

printf("%d\n", num);

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: