hdu 1241--入门DFS
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11984 Accepted Submission(s): 6952
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.
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.
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
0
1
2
2
题目大意:找出连续相连的@有几条 由于我是英文渣题目都没看清楚就做 错了好久,还理解错了,待会会讲我错哪了;;;;;= 。=
AC代码:
//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;
}
我之前的做法(错的):
错误:一开始没注意到对角线也可以 后来修正了,但是还是WA
错误2:WA的原因就是我多此一举还用hash存 我看错题目以为相同长度的只能算一次 (T_T只看样例去理解题目意思的好桑不起啊~~)
//============================================================================
// Name : 2333.cpp
// Author : zjx
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================ #include <iostream>
using namespace std;
char op[105][105];
bool num[105][105];
bool hash[10010];
int dfs(int n,int m){
if(num[n][m]||op[n][m]=='*'){
return 0;
}
num[n][m]=true;
int sum;
sum=1+dfs(n-1,m)+dfs(n+1,m)+dfs(n,m+1)+dfs(n,m-1)+dfs(n+1,m+1)+dfs(n-1,m-1)+dfs(n+1,m-1)+dfs(n-1,m+1);
return sum;
}
int main()
{
int a,b,i,j;
while(cin>>a>>b){
if(a==0&&b==0)break;
memset(num,false,sizeof(num));
memset(hash,false,sizeof(hash));
for(i=1;i<=a;i++)
for(j=1;j<=b;j++){
cin>>op[i][j];
}
int sum=0;int temp;
for(i=0;i<=a+1;i++){op[i][0]='*';op[i][b+1]='*';}
for(j=0;j<=b+1;j++){op[0][j]='*';op[a+1][j]='*';}
for(i=1;i<=a;i++)
for(j=1;j<=b;j++){
temp=dfs(i,j);
if(temp!=0&&hash[temp]==false){
sum++;hash[temp]=true;
}
}
cout<<sum<<endl;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
hdu 1241--入门DFS的更多相关文章
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
- hdu 1241 Oil Deposits(DFS求连通块)
HDU 1241 Oil Deposits L -DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & ...
- HDOJ(HDU).1241 Oil Deposits(DFS)
HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- DFS(连通块) HDU 1241 Oil Deposits
题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...
- Oil Deposits HDU - 1241 (dfs)
Oil Deposits HDU - 1241 The GeoSurvComp geologic survey company is responsible for detecting undergr ...
- 深搜基础题目 杭电 HDU 1241
HDU 1241 是深搜算法的入门题目,递归实现. 原题目传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1241 代码仅供参考,c++实现: #incl ...
- HDU 1241 Oil Deposits(石油储藏)
HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Probl ...
- HDOJ(HDU).1015 Safecracker (DFS)
HDOJ(HDU).1015 Safecracker [从零开始DFS(2)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双重DFS HDOJ.1 ...
- HDU.5692 Snacks ( DFS序 线段树维护最大值 )
HDU.5692 Snacks ( DFS序 线段树维护最大值 ) 题意分析 给出一颗树,节点标号为0-n,每个节点有一定权值,并且规定0号为根节点.有两种操作:操作一为询问,给出一个节点x,求从0号 ...
- HDU 1241 连通块问题(DFS入门题)
Input The input file contains one or more grids. Each grid begins with a line containing m and n, th ...
随机推荐
- Testing for diversifying selection for two clades with a background clade
在利用branch-site检测趋同进化的时候 .可以将各个趋同进化枝分别进行检测,分析的时候不去除某一趋同枝系 .在分析的时候,需要去除其他趋同枝系的影响 I have sequences of a ...
- 20155304 2016-2017-2 《Java程序设计》第八周学习总结
20155304 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 NIO NIO使用频道来衔接数据节点,在处理数据时,NIO可以让你设定缓冲区容量,在缓冲区中 ...
- 20155307 2017-2018-2 《Java程序设计》第2周学习总结
20155307 2017-2018-2 <Java程序设计>第2周学习总结 教材学习内容总结 整数:short(2 byte).int(4 byte).long(8 byte) 字节(b ...
- 20155317 2016-2017-2 《Java程序设计》第十学习总结
20155317 2016-2017-2 <Java程序设计>第十学习总结 教材学习内容总结 1.网络编程的概念: 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作 ...
- 20155327 2016-2017-2 《Java程序设计》第一周学习总结
20155327 2016-2017-2 <Java程序设计>第一周学习总结 教材学习内容总结 浏览教材,根据自己的理解每章提出一个问题 1.JAVA SE中JVM,JRE与JDK分别是什 ...
- 20155334 2016-2017-2 《Java程序设计》第三周学习总结
20155334 2016-2017-2 <Java程序设计>第三周学习总结 教材学习内容总结 第四章: 讲的是类类型,使用java撰写程序几乎都在使用对象(Object),要产生对象必须 ...
- 北京Uber优步司机奖励政策(4月10日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 【转载】从零实现3D图像引擎:(1)环境配置与项目框架
原文:从零实现3D图像引擎:(1)环境配置与项目框架 0. 要学懂3D程序设计,必然要精通3D相关的线性代数.3D几何.复分析等相关知识,我也因为如此才开始这个博客系列的写作,不自己实现,就不是自己的 ...
- 存一下emacs配置
(global-set-key [f9] 'compile-file)(global-set-key [f10] 'gud-gdb)(global-set-key (kbd "C-z&quo ...
- 【MongoDB安装】MongoDB在centos linux平台安装
参考:http://www.runoob.com/mongodb/mongodb-linux-install.html 一..下载安装包 下载方式: 1.登录官网download,然后通过xftp传到 ...