HDOJ/HDU 1241 Oil Deposits(经典DFS)
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
0
1
2
2
题意:
*代表荒地,@代表油田。@的上下左右,还有对角的4个如果还有@,就表示它们是一个油田,问-给出的图中,一共有多少油田。
分析:
本题用DFS(深搜)可以很好的解决问题!
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
char map[210][210];
int df[210][210];
int con,n,m;
void dfs(int x,int y,int t){
if(df[x][y]==1||x<0||y<0||x>=n||y>=m){
return;
}
if(map[x][y]=='*'){
return;
}
if(t==1&&map[x][y]=='@'){
con++;
//printf("con=%d\n",con);
}
//printf("%d %d\n",x,y);
if(map[x][y]=='@'){
map[x][y]='*';
df[x][y]=1;
dfs(x+1,y+1,0);
dfs(x-1,y-1,0);
dfs(x+1,y,0);
dfs(x,y+1,0);
dfs(x-1,y,0);
dfs(x,y-1,0);
dfs(x+1,y-1,0);
dfs(x-1,y+1,0);
df[x][y]=0;
}
}
int main()
{
while(~scanf("%d%d",&n,&m),(n||m)){
memset(df,0,sizeof(df));
con=0;
getchar();
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%c",&map[i][j]);
}
getchar();
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
dfs(i,j,1);
}
}
printf("%d\n",con);
}
return 0;
}
HDOJ/HDU 1241 Oil Deposits(经典DFS)的更多相关文章
- HDU - 1241 Oil Deposits 经典dfs 格子
最水的一道石油竟然改了一个小时,好菜好菜. x<=r y<=c x<=r y<=c x<=r y<=c x<=r y<=c #include ...
- HDOJ(HDU).1241 Oil Deposits(DFS)
HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- hdu 1241 Oil Deposits(DFS求连通块)
HDU 1241 Oil Deposits L -DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & ...
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
- hdu 1241:Oil Deposits(DFS)
Oil Deposits Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- HDU 1241 Oil Deposits (DFS)
题目链接:Oil Deposits 解析:问有多少个"@"块.当中每一个块内的各个"@"至少通过八个方向之中的一个相邻. 直接从"@"的地方 ...
- HDU 1241 Oil Deposits【DFS】
解题思路:第一道DFS的题目--- 参看了紫书和网上的题解-- 在找到一块油田@的时候,往它的八个方向找,直到在能找到的范围内没有油田结束这次搜索 可以模拟一次DFS,比如说样例 在i=0,j=1时, ...
- HDU - 1241 Oil Deposits 【DFS】
题目链接 https://cn.vjudge.net/contest/65959#problem/L 题意 @表示油田 如果 @@是连在一起的 可以八个方向相连 那么它们就是 一块油田 要找出 一共有 ...
- DFS(连通块) HDU 1241 Oil Deposits
题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...
随机推荐
- 03_JqueryAjax_异步请求Servlet
[Ajax 简述] jquery对Ajax提供了更方便的代码:$ajax({ops})来发送异步请求. 首先说一个Ajax的特性,它是永安里发送异步请求,请求的是服务器,但不会刷新页面. 例如在注册功 ...
- Codevs 3287 货车运输 2013年NOIP全国联赛提高组(带权LCA+并查集+最大生成树)
3287 货车运输 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 传送门 题目描述 Description A 国有 n 座 ...
- HDU_2014 青年歌手大奖赛_评委会打分
Problem Description 青年歌手大奖赛中,评委会给参赛选手打分.选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程输出某选手的得分. Input 输入数据有多组,每 ...
- 《sed的流艺术之三》-linux命令五分钟系列之二十三
本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...
- eclipse汉化安装
http://hi.baidu.com/rankabc/item/b07e03466550e4ce1381dac4 Eclipse汉化方法 网上搜了很多教程,步骤都不够详细,因此写一篇傻瓜版教程让新手 ...
- top工具
top 显示进程所占系统资源 能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. top命令打印出了很多信息,包括系统负载(loadaverage).进程数(Tasks).c ...
- bzoj 1964: hull 三维凸包 计算几何
1964: hull 三维凸包 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 54 Solved: 39[Submit][Status][Discuss ...
- The Bellman-Ford algorithm
This algorithm deals with the general case, where G is a directed, weight graph, and it can contains ...
- UITableView.m:8042 crash 崩溃
CRASH : /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.6.21/UITableView.m:804 ...
- AJAX里调用AJAX,作定时进度刷新
这个确实搞了一段时间,但成就感有啦... 哈哈,这个自动部署平吧,异步队列CELERY+REDIS,发布进度实时AJAX的技术点全部打通!!! 而获取实时进度,我用的是RESTFUL FRAMEWOR ...