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 ...
随机推荐
- GitLab私服在Ubuntu上搭建总结
1.前期准备: sudo apt-get update sudo apt-get install -y curl openssh-server ca-certificates sudo apt-get ...
- Soldier and Number Game CodeForces - 546D 素因子数打表(素数筛的改造)
题意: 输入 a 和 b(a>b),求a! / b!的结果最多能被第二个士兵给的数字除多少次. 思路: a! / b!肯定能把b!约分掉,只留下b+1~a的数字相乘,所以我们求b+1 ~ a的所 ...
- adb的基本安装和介绍(一)
一,什么是adb? adb全称为Android Debug Bridge,就是起到调试桥的作用.顾名思义,adb就是android sdk 的一个工具 借助adb工具,我们可以管理设备或手机模拟器的状 ...
- ATM 最初版
# 二 一个项目开发前,有一份开发文档 # # 项目: 编写小说阅读程序实现下属功能 # # # 一:程序运行开始时显示 # 0 账号注册 # 1 账号登录 # 2 充值功能 # 3 阅读小说 imp ...
- js 中的yield
yield是什么 yield是ES6的新关键字,使生成器函数执行暂停,yield关键字后面的表达式的值返回给生成器的调用者.它可以被认为是一个基于生成器的版本的return关键字. yield关键字实 ...
- Reface.AppStarter 框架初探
Reface.AppStarter 是一种基于 .NetFramework 的应用程序启动模式,使用该启动模式,你可以轻松的得到以下功能 : IOC / DI 自动注册与装配 简化配置 垂直模块化你的 ...
- Kaggle竞赛入门(二):如何验证机器学习模型
本文翻译自kaggle learn,也就是kaggle官方最快入门kaggle竞赛的教程,强调python编程实践和数学思想(而没有涉及数学细节),笔者在不影响算法和程序理解的基础上删除了一些不必要的 ...
- 【译】Java SE 14 Hotspot 虚拟机垃圾回收调优指南
原文链接:HotSpot Virtual Machine Garbage Collection Tuning Guide,基于Java SE 14. 本文主要包括以下内容: 优化目标与策略(Ergon ...
- HTML特殊转义字符——特殊符号
干货,见下图: 后期我会陆续更一些JavaScript的文章,大家可以一起学习交流.
- 2019NYIST计科第七次周赛总结
2019NYIST计科第七次周赛总结 文章目录 2019NYIST计科第七次周赛总结 [秤取物体重量( 二进制枚举法)](https://blog.csdn.net/qq_34261446/artic ...