poj1562 Oil Deposits(DFS)
题目链接
http://poj.org/problem?id=1562
题意
输入一个m行n列的棋盘,棋盘上每个位置为'*'或者'@',求'@'的连通块有几个(连通为8连通,即上下左右,两条对角线)。
思路
floodfill问题,用dfs解决。
代码
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; const int N = ;
char grid[N][N];
int visit[N][N];
int dir[][] = { //八个邻接点
{, }, {-, }, {, }, {, -},
{-, -}, {-, }, {, }, {, -}
};
int m, n; void dfs(int r, int c)
{
for(int i=; i<; i++)
{
int nr = r+dir[i][];
int nc = c+dir[i][]; if(nr>= && nr<m && nc>= && nc<n && grid[nr][nc]=='@' && !visit[nr][nc])
{
visit[nr][nc] = ;
dfs(nr, nc);
}
}
} int main()
{
//freopen("poj1562.txt", "r", stdin);
while(cin>>m>>n && m)
{
for(int i=; i<m; i++)
cin>>grid[i]; memset(visit, , sizeof(visit));
int cnt = ;
for(int i=; i<m; i++)
{
for(int j=; j<n; j++)
{
if(grid[i][j]=='@' && !visit[i][j])
{
visit[i][j] = ;
dfs(i, j);
cnt++;
}
}
}
cout<<cnt<<endl;
}
return ;
}
poj1562 Oil Deposits(DFS)的更多相关文章
- 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)
题目链接:Oil Deposits 解析:问有多少个"@"块.当中每一个块内的各个"@"至少通过八个方向之中的一个相邻. 直接从"@"的地方 ...
- UVA - 572 Oil Deposits(dfs)
题意:求连通块个数. 分析:dfs. #include<cstdio> #include<cstring> #include<cstdlib> #include&l ...
- UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)
UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...
- hdu 1241 Oil Deposits(DFS求连通块)
HDU 1241 Oil Deposits L -DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & ...
- ZOJ 1709 Oil Deposits(dfs,连通块个数)
Oil Deposits Time Limit: 2 Seconds Memory Limit: 65536 KB The GeoSurvComp geologic survey compa ...
- 【HDU - 1241】Oil Deposits(dfs+染色)
Oil Deposits Descriptions: The GeoSurvComp geologic survey company is responsible for detecting unde ...
- Oil Deposits(DFS连通图)
Description The GeoSurvComp geologic survey company is responsible for detecting underground oil dep ...
- Oil Deposits(油田)(DFS)
题目: The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. G ...
随机推荐
- nova-conductor与AMQP(一)
源码版本:H版 一.AMQP基础 1. 什么是AMQP 可以参考如下文章: http://blog.csdn.net/linvo/article/details/5750987 http://blog ...
- 学习 C++的用途,(前辈总结)
C++准确说是一门中级语言,介于汇编和高级语言之间吧,要求程序员了解计算机的内部数据存储.个人认为,作为学生还是花功夫学C++,因为<设计模式><数据结构>这些课程基本上还是C ...
- 2015/11/3用Python写游戏,pygame入门(3):字体模块、事件显示和错误处理
游戏里面一般是肯定会出现文字显示的,即使是俄罗斯方块也应该显示分数.那么我们应该怎样来显示文字呢,今天一起学习一下pygame的font模块. 使用字体模块 pygame可以直接调用系统字体,也可以调 ...
- TED_Topic1:Why we need to rethink capitalism
Topic 1:Why we need to rethink capitalism By Paul Tudor Jones II # Background about our speaker ...
- 51nod1110 距离之和最小 V3
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].该点到其他点的带权距离 = 实际距离 * 权值.求X轴上 ...
- 引用类型 ( 对象定义 )——Object 类型
本文地址:http://www.cnblogs.com/veinyin/p/7607100.html 创建实例 new 操作符后跟构造函数 var people = new Object(); pe ...
- a 标签传值
转载:http://blog.csdn.net/muyeju/article/details/48594377 .<a>标签传值的形式--参数固定:<a href="地址? ...
- python学习笔记(十三)之lambda表达式
lambda表达式: 用法 lambda x : 2 * x + 1 其中:前面是参数,后面是返回值. >>> def ds(x): ... return 2 * x + 1 ... ...
- python作业员工信息表程序(第四周)
作业需求: 1. 员工信息表程序,实现增删改查操作: 2. 可进行模糊查询,语法至少支持下面3种: select name,age from staff_table where age > 22 ...
- 安装JDK环境变量的配置
设置环境变量 在java中需要设置三个环境变量(1.5之后不用再设置classpath了,但是个人强烈建议继续设置以保证向下兼容问题) JDK安装完成之后我们用来设置环境变量:右击”我的电脑“,选择” ...