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 ...
随机推荐
- 装numpy 环境:python3.4+ windows7 +64位系统
机器学习实战python 因为图像处理的原因,初步学习机器学习,选用语言python,参考书籍<机器学习实战> 环境:python3.4+ windows7 +64位系统 首先,今天解决的 ...
- JavaScript DOM 事件模型
JavaScript DOM 事件模型 JavaScript 是基于面向对象和事件驱动的一门语言,事件模型是 DOM 中至关重要的内容,理解事件驱动机制.事件反馈.事件冒泡.事件捕获以及事件委托能帮助 ...
- EF多租户实例:快速实现分库分表
前言 来到这篇随笔,我们继续演示如何实现EF多租户. 今天主要是演示多租户下的变形,为下图所示 实施 项目结构 这次我们的示例项目进行了精简,仅有一个API项目,直接包含所有代码. 其中Control ...
- IdentityServer 部署踩坑记
IdentityServer 部署踩坑记 Intro 周末终于部署了 IdentityServer 以及 IdentityServerAdmin 项目,踩了几个坑,在此记录分享一下. 部署架构 项目是 ...
- IBN-Net: 提升模型的域自适应性
本文解读内容是IBN-Net, 笔者最初是在很多行人重识别的库中频繁遇到比如ResNet-ibn这样的模型,所以产生了阅读并研究这篇文章的兴趣,文章全称是: <Two at Once: Enha ...
- js函数基础回顾
回头又跑去看了下尚硅谷的js基础视频 https://www.bilibili.com/video/av22958172/?p=51. 便做了如下笔记: 1.函数也是一个对象 2.函数可以封装一些功能 ...
- Redis新手的坑,无法连接、缺少类、没有密码
阿西吧,自己太菜了,用SpringBoot调用Redis缓存的时候,出了各种问题,记录一下,大家别学我 本文有自己的经验,也有从其他博客整理来的零零散散的经验. 1.安装Redis 安装redis,一 ...
- for、forEach、for-in与for-of的区别
let arr=[1,2,3,4,5]; arr.b='100'; for for(let i=0;i<arr.length;i++){ console.log(arr[i]); } for是编 ...
- Centos装机预备技能
装机预备技能 1.1问题 本例要求安装一台可用的KVM服务器: RHEL与Cent ...
- abp(net core)+easyui+efcore实现仓储管理系统——入库管理之八(四十四)
abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...