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 ...
随机推荐
- 10.Android UiAutomator Junit 断言函数的使用
一.断言函数介绍 1.断言函数: 确定被测试的方法是否按照预期的效果正常工作 比如说: if (假设成立){ 通过测试 }else{ 报错并终止当前用例测试 } 2.断言函数用例结构: 一个完整的测试 ...
- scp 从本地往线上传文件
scp /home/wwwroot/default/tf_ment.sql root@IP:/home/wwwroot/default/
- java rmi远程方法调用实例
RMI的概念 RMI(Remote Method Invocation)远程方法调用是一种计算机之间利用远程对象互相调用实现双方通讯的一种通讯机制.使用这种机制,某一台计算机上的对象可以调用另外一台计 ...
- Oracle把本地的dmp备份文件导入到本地的Oracle数据库中语句
----------------------------------------------------------------------------- 导入语法 imp usename/passw ...
- 2017国庆 清北学堂 北京综合强化班 Day1
期望得分:60+ +0=60+ 实际得分:30+56+0=86 时间规划极端不合理,T2忘了叉积计算,用解析几何算,还有的情况很难处理,浪费太多时间,最后gg 导致T3只剩50分钟,20分钟写完代码, ...
- Flex布局(伸缩盒布局)
Flexible Box是什么?Flexible意为可伸缩的,Box意为盒子,可以理解为一种新式的盒模型——伸缩盒模型.由CSS3规范提出,这是在原有的大家非常熟悉的block, inline-blo ...
- js父页面和子页面相互取值
iframe子页面与父页面通信根据iframe中src属性是同域链接还是跨域链接,通信方式也不同. 一.同域下父子页面的通信 父页面parent.html <html> <head& ...
- 关于linux下crontab mysql备份出来的数据为0字节的问题
问题出在计划任务所执行的脚本上! 脚本中的调用的指令应该都写全路径~ 实例: # crontab -c 编辑下的内容 30 18 * * * /root/backup.sh 意思为:每天18:30执行 ...
- Java面试中常问的Spring方面问题(涵盖七大方向共55道题,含答案)
1.一般问题 1.1. 不同版本的 Spring Framework 有哪些主要功能? VersionFeatureSpring 2.5发布于 2007 年.这是第一个支持注解的版本.Spring 3 ...
- python 第二章 对象与类型
可变对象和不可变对象 1,可变对象,list(列表),dict(字典),集合(set),字节数组. 2,不可变对象,数值类型,字符串,字节串,元组(具体形式 ()). 注意条件:可变和不可变指的是该对 ...