Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3087 Accepted Submission(s): 1765
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.
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.
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.
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
说到深搜,就是所谓的dfs,我首先就是想到这一题,因为我做的第一道深搜题就是这一道,给我非常大的启发,原来深搜(dfs)就是如此!!!
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241
我的代码:
//Oil Deposits
#include <iostream>
#include <stdio.h>
using namespace std;
char map[101][101];
int n, m, sum;
void dfs(int i, int j)
{
//若该点不可行或越界,返回
if(map[i][j]!='@' || i<0 || j<0 || i>=m || j>=n) return;
else //否则,标记该点为不可行,再往8个方向深搜
{
map[i][j] = '!';
dfs(i-1, j-1);
dfs(i-1, j);
dfs(i-1, j+1);
dfs(i, j-1);
dfs(i, j+1);
dfs(i+1, j-1);
dfs(i+1, j);
dfs(i+1, j+1);
}
}
int main()
{
int i, j;
while(cin>>m>>n)
{
if(m==0 || n==0) break;
sum = 0;
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
cin>>map[i][j];
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
if(map[i][j] == '@')
{
dfs(i, j);
sum++;
}
}
}
cout<<sum<<endl;
}
return 0;
}
分享到:
相关推荐
搜索 dfs 解题代码 hdu1241
HDU最全ac代码
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
HDU图论题目分类
杭电ACMhdu1163
HDU1059的代码
hdu1001解题报告
hdu 1574 passed sorce
HDU的一题........HDU DP动态规
八数码的A*算法~不是很高效,但是很适合刚刚学这个算法的朋友们
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu2101AC代码
hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。
hdu acm 教案 动态规划(1) hdu acm 教案 动态规划(1)
ACM HDU题目分类,我自己总结的大概只有十来个吧
hdu 1166线段树代码
hdu动态规划算法集锦
自己做的HDU ACM已经AC的题目
hdu题目分类
关于hdu的动态规划的题目,包括一些水题,还有一些经典的动态规划题目。