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 ...
随机推荐
- myeclipse注册码,可以用到2016年
myeclipse注册码,可以用到2016年 xiangyang kLR8ZF-655954-61677756068297221
- echarts 分组绘制柱状图
示意图: 代码如下: <!DOCTYPE html> <html style="height: 100%"> <head> <meta c ...
- JNI实现JAVA和C++互相调用
SDK.h #ifndef SDK_H #define SDK_H #include "AsyncProxy.h" #include "Module.h" #i ...
- R3—日期处理
一. 问题引入 下面是一个房地产价格数据,现在想要提取2008年6月份的数据进行分析,在R中该如何操作呢? city price bedrooms squarefeet lotsize latitud ...
- python 版本zabbix_sender
python版本的zabbix_sender: 使用方法: 1.导入 : from zbx_send import info 2.实例化: test=info() 3.支持 ...
- Metasploit 一些重要模块使用介绍
本文是"T00LS Metasploit(第一季)"的文档版,是个人在观看视频动手操作的一个记录,仅供学习.文中会介绍Metasploit的一些基本使用:端口扫描.smb扫描.服务 ...
- Linux终端提示符PS1设置(颜色)
\d :代表日期,格式为weekday month date,例如:"Mon Aug 1"\H :完整的主机名称.例如:我的机器名称为:fc4.linux,则这个名称就是fc4.l ...
- VueJS 轻松支持 JSX 配置
使用: babel-preset-vue-app TODO
- Supply
Supplier创建一个Supply Supply有tap或emit方法. 可以这样理解: Supplier创建一个工厂 Supply 用tap创建流水线 emit向流水线上传送加工品进行加厂 my ...
- 使用批处理方式从svn 检出DEMO
Branching in Subversion¶ FROM:https://dev.geogebra.org/trac/wiki/SubversionBranching Some people wan ...