Program2_1011
2016-04-15 21:17
232 查看
我现在做的是第二专题编号为1011的试题,试题内容如下所示:
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]
[align=left]Sample Output[/align]
简单题意:
探索油田的数量,用@表示存在油田,*表示不存在油田,如果两个油田相邻,则将这两个看成一个油田,计算油田的数量
解题思路:
实际上这几个题的本质都是利用深度优先搜索进行计算油田的数量,建立一个 标记数组,再建立一个数组进行保存油田信息,然后建立一定的规则对数组的每一个数组进行搜索,看是否满足要求,对已经访问过的位置进行标记,以免重复搜索。
编写代码:
#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;
}
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;
}
相关文章推荐
- 3. NHibernate基础知识 - 你必须知道的一些事情
- eclipse maven 项目导出为 jar 包
- CSS 在编写时要注意的15个问题
- 数据库连接及操作
- Lightoj1393(博弈+NIM)
- JDK5新特性之增强for
- httpclient4.1使用详解
- hdu 2132... 被基本问题考住了。。
- poj3468 线段树
- NavigationDrawe 实践
- 练习二1009
- 如何使用R语言的Boruta包进行特征选择
- ACM--枚举+DFS/巧妙思想--POJ--2965--The Pilots Brothers' refrigerator--初期算法
- VS2012——由于缺少目标xx.exe,Visual Studio
- Spring项目的建立-移植流程(非入门教程)
- Spring项目的建立-移植流程(非入门教程)
- 面试题:两个栈实现队列
- 放苹果
- 计蒜客 挑战难题 单独的数字
- error C2664 无法将参数 从“const char []”转换为“LPCTSTR