poj1562 DFS入门
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
题目大意:就是给你一个矩阵形地图,用@表示油井,*表示空白处,每一个油井周围那8个位置都算与他相邻,所有相邻的油井算作一个pocket,问有多少个pocket.
思路分析:这应该算是BFS题目中最简单的一类了,直接暴力过一遍,每次都从@进行遍历,计数加1,然后深搜将所有搜索到的@都标记为*,当所有的@都被标记,搜索结束,输出计数变量。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
const int maxn=105;
char ma[maxn][maxn];
int f[8][2]={{1,0},{-1,0},{0,1},{0,-1},{-1,-1},{-1,1},{1,-1},{1,1}};
int m,n;
int cnt,flag;
void dfs(int x,int y)
{
if(!flag)
flag=1,cnt++;
ma[x][y]='*';
for(int i=0;i<8;i++)
{
int a=x+f[i][0];
int b=y+f[i][1];
if(ma[a][b]=='@')
dfs(a,b);
}
}
int main()
{
while(cin>>m>>n&&(m||n))
{
cnt=0;
int i,j;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
cin>>ma[i][j];
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
flag=0;
if(ma[i][j]=='@')
dfs(i,j);
}
}
cout<<cnt<<endl;
}
}
人一我百,人百我千!
poj1562 DFS入门的更多相关文章
- 算法学习之BFS、DFS入门
算法学习之BFS.DFS入门 0x1 问题描述 迷宫的最短路径 给定一个大小为N*M的迷宫.迷宫由通道和墙壁组成,每一步可以向相邻的上下左右四格的通道移动.请求出从起点到终点所需的最小步数.如果不能到 ...
- Oil Deposits(poj 1526 DFS入门题)
http://poj.org/problem?id=1562 ...
- DFS入门之二---DFS求连通块
用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...
- DFS入门之一
深度优先搜索实现较为简单,需要控制两个因素: 1.已经访问过的元素不能再访问,在实际题目中还要加上不能访问的元素(障碍) 2.越界这种情况是不允许的 以杭电的1312 Red and Black 为例 ...
- [HDU]1016 DFS入门题
题目的意思就是在1到n的所有序列之间,找出所有相邻的数相加是素数的序列.Ps:题目是环,所以头和尾也要算哦~ 典型的dfs,然后剪枝. 这题目有意思的就是用java跑回在tle的边缘,第一次提交就tl ...
- DFS入门__poj1979
Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 26944 Accepted: 14637 D ...
- POJ 3984(DFS入门题 +stack储存路径)
POJ 3984 Description 定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, ...
- HDU 1241 连通块问题(DFS入门题)
Input The input file contains one or more grids. Each grid begins with a line containing m and n, th ...
- POJ 1416 Shredding Company【dfs入门】
题目传送门:http://poj.org/problem?id=1416 Shredding Company Time Limit: 1000MS Memory Limit: 10000K Tot ...
随机推荐
- DX笔记之五------游戏画面绘图之绘制位图
本系列文章由zhmxy555编写,转载请注明出处. http://blog.csdn.net/zhmxy555/article/details/7335103 共四步 步骤一:加载位图 步骤二:建立与 ...
- Java系列--第三篇 基于Maven的Android开发CAIO
学习要打好基础,这里用一个项目来学习一下Android的组件,参考网址为这个但不限于这个.有些东西的学习,理解三遍理论还不如一遍操作,所谓理论来自实践,实践是检验真理的唯一标准.所以,虽然看懂了那篇文 ...
- python学习第七天 -- dict 和set
今天主要学习关于python 的dict(全称dictionary)和set.dict的用法跟javascript 中map表类似,key + value结构语言.而set,准确来说,只是key的集合 ...
- Activiti工作流学习-----基于5.19.0版本(4)
四.使用工作流开发 org.activiti.engine.ProcessEngine提供的Service作用在工作流引擎上面,如果所示是模仿一个公司简单的审批流程,你可以下载这个Demo:Activ ...
- AFNetworking 系列教程
前几天发现iOS9.xcode7之后NSURLConnection的API被弃用了,取而代之的是NSURLSession,看到这我首先想到了AFNetworking,因为工程的所有网络请求都是基于 ...
- hdu 畅通工程再续
http://acm.hdu.edu.cn/showproblem.php?pid=1875 #include <cstdio> #include <cstring> #inc ...
- WPF:如何实现单实例的应用程序(Single Instance)
原文:WPF:如何实现单实例的应用程序(Single Instance) 好吧,这是我将WPF与Windows Forms进行比较的系列文章的第四篇,讨论一下如何实现单实例(single instan ...
- 关于selenium中的sendKeys()隔几秒发送一个字符
看一下你的IEDriverServer.exe是不是64位的,我也遇到了这样的问题,换成32位的IEDriverServer.exe,瞬间速度快了
- 全球扫货指南:面向中国消费者的全球奢侈品旅游导购服务 |PingWest中文网
全球扫货指南:面向中国消费者的全球奢侈品旅游导购服务 |PingWest中文网 用"全球扫货指南
- [转载]Linux的时间与时钟中断处理
本文主要介绍在Linux下的时间实现以及系统如何进行时钟中断处理. 一. Linux的硬件时间 PC机中的时间有三种硬件时钟实现,这三种都是基于晶振产生的方波信号输入.这三种时钟为: 实时时钟RTC ...