A - Oil Deposits DFS
InputThe 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.
OutputFor 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
思路 : 这是一道特别经典的DFS题目 就是先找到一个@然后将与@相连的@全部变成“×” dfs结束,说明没有与@相连的点了 计一此数
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int n,m;
char arr[][];
int mark[][]={};
int d[][]={{,},{,},{,-},{-,},{,},{,-},{-,},{-,-}};
void dfs(int x,int y){
mark[x][y]=;
for(int i=;i<;i++){
int dx=x+d[i][];
int dy=y+d[i][];
if(dx>=&&dy>=&&dx<n&&dy<m&&mark[dx][dy]==&&arr[dx][dy]=='@'){
arr[dx][dy]='*';
dfs(dx,dy);
}
}
}
int main()
{
while(cin>>n>>m&&m){
int ans=;
memset(mark,,sizeof(mark));
for(int i=;i<n;i++)
scanf("%s",&arr[i]);
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
if(arr[i][j]=='@')
{
dfs(i,j);
ans++;
}
}
cout<<ans<<endl;
} return ;
}
A - Oil Deposits DFS的更多相关文章
- HDOJ(HDU).1241 Oil Deposits(DFS)
HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- Oil Deposits(dfs)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- UVa572 Oil Deposits DFS求连通块
技巧:遍历8个方向 ; dr <= ; dr++) ; dc <= ; dc++) || dc != ) dfs(r+dr, c+dc, id); 我的解法: #include< ...
- HDU 1241 Oil Deposits (DFS/BFS)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- 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深搜)
Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...
- UVa 572 Oil Deposits(DFS)
Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil ...
- [POJ] 1562 Oil Deposits (DFS)
Oil Deposits Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16655 Accepted: 8917 Des ...
- Oil Deposits(dfs水)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...
随机推荐
- hdu3665Floyd解法
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/3665/ Floyd是经典的dp算法,将迭代过程分成n个阶段,经过n个阶段的迭代所有点对之间的最短路径都可以求出, ...
- CF 631C report
Each month Blake gets the report containing main economic indicators of the company "Blake Tech ...
- 一个完整的机器学习项目在Python中演练(三)
大家往往会选择一本数据科学相关书籍或者完成一门在线课程来学习和掌握机器学习.但是,实际情况往往是,学完之后反而并不清楚这些技术怎样才能被用在实际的项目流程中.就像你的脑海中已经有了一块块"拼 ...
- PMP学习笔记(一)
前9节列举出了很多例子来辅助理解什么是项目管理,在学习的过程当中听到了一些名词,查询过一些资料之后,在这里梳理出来 1.关键路径法 关键路径是指设计中从输入到输出经过的延时最长的逻辑路径.优化关键路径 ...
- C# 添加、修改、删除PPT中的超链接
本文介绍通过C# 编程如何在PPT幻灯片中添加超链接的方法,添加链接时,可给文本或者图片添加超链接,链接对象可指向网页地址.邮件地址.指定幻灯片等,此外,也可以参考文中编辑.删除幻灯片中已有超链接的方 ...
- TCP连接与HTTP请求
一道经典面试题: 从 URL 在浏览器被被输入到页面展现的过程中发生了什么? 相信大多数准备过的同学都能回答出来,但是如果继续问:收到的 HTML 如果包含几十个图片标签,这些图片是以什么方式.什么顺 ...
- WiX 自定义
WiX 允许用户做一些自定义操作 用户界面向导 对于安装界面,用户可以用自己的位图.图标和许可证文本替换默认的.它们的路径存储在变量中,您可以在命令行或直接在源代码中指定这些变量: <WixVa ...
- Python语法元素分析
缩进 1个缩进 = 4个空格 用以在Python中标明代码的层次关系 缩进是Python语言中表明程序框架的唯一手段 注释 注释:程序员在代码中加入的说明信息,不被计算机执行 注释的两种方法: 单行注 ...
- Sublime Text 2 Install Package Debug
本文转载自CSDN空间freshlover的博客<Sublime Text 无法使用Package Control或插件安装失败的解决方法>,转载请注明出处,谢谢! Sublime Tex ...
- docker-compose搭建redis哨兵集群
头脑风暴 出于学习目的,您可以很轻松地在docker环境下运行redis的单个实例,但是如果您需要在生产环境中运行它,那么必须将Redis部署为HA(High Avaliable)模式. Redis ...