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求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...
随机推荐
- javascript 返回顶部
<style> #linGoTopBtn { POSITION: fixed; TEXT-ALIGN: center; LINE-HEIGHT: 30px; WIDTH: 30px; ...
- SqlServer日期(convert函数,getdate函数)
SqlServer日期(convert函数,getdate函数) 函数GETDATE()的返回值在显示时只显示到秒.实际上,SQL Sever内部时间可以精确到毫秒级(确切地说,可以精确到3.33毫秒 ...
- js 中如何通过提示框跳转页面
通过提示框跳转页面 <!doctype html> <html lang="en"> <head> <meta charset=" ...
- Python全栈开发之MySQL(二)------navicate和python操作MySQL
一:Navicate的安装 1.什么是navicate? Navicat是一套快速.可靠并价格相宜的数据库管理工具,专为简化数据库的管理及降低系统管理成本而设.它的设计符合数据库管理员.开发人员及中小 ...
- magento install
1: apt-get insatll apache2 mysql php5 2: 把下载的magneto 解压后放到 /var/www/magento 3: 进入 http://127. ...
- 详解 CSS 属性 - 伪类和伪元素的区别[转]
首先,阅读 w3c 对两者的定义: CSS 伪类用于向某些选择器添加特殊的效果. CSS 伪元素用于将特殊的效果添加到某些选择器. 可以明确两点,第一两者都与选择器相关,第二就是添加一些“特殊”的效果 ...
- 进位位(carry)与溢出位(overflow)的区别
处理器内部以补码表示有符号数,8个二进制位能够表达的整数范围是:+127 ~ -128,16位表达的范围是:+32767 ~ -32768.如果运算结果超出了这个范围,就是产生了溢出:有溢出,说明有符 ...
- 数据采集服务提供商,ip提供商 里面有些不错的基础数据
http://user.qzone.qq.com/1649677458 这家公司的爬虫应该挺牛的 !@#!#!~#¥¥¥@@http://www.site-digger.com/
- Central Europe Regional Contest 2012 Problem J: Conservation
题目不难,感觉像是一个拓扑排序,要用双端队列来维护: 要注意细节,不然WA到死 = =! #include<cstdio> #include<cstring> #includ ...
- django ORM中update_or_create功能,如果只要匹配某一特定字段呢
今天发现的需求,在官方文档找到说法: In English, that means start with any non-'defaults' keyword argument that doesn’ ...