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 ...
随机推荐
- bzoj 1004 组合
代码: //根据Burnside定理:有m个置换k钟颜色,所有本质不同的染色方案数就是每种置换的不变元素的个数的平均数.所谓不变元素就是一种染色方案 //经过置换变换后和之前一样.所以现在就是要求不变 ...
- mysql 给用户赋值权限
解决办法 grant all privileges on *.* to joe@localhost identified by '1'; flush privileges; 拿 joe 1 登陆 附: ...
- Nginx -- proxy_pass配置
一.proxy_pass 作用域: location 不影响浏览器地址栏的url 设置被代理server的协议和地址 协议可以为http或https 地址可以为域名或IP 二.配置规则 2.1 测试环 ...
- 「Linux」centos7安装mysql
1.yum仓库下载MySQL:sudo yum localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch. ...
- ASP.Net中自定义Http处理及应用之HttpModule篇
HttpHandler实现了类似于ISAPI Extention的功能,他处理请求(Request)的信息和发送响应(Response).HttpHandler功能的实现通过实现IHttpHandle ...
- [linux]ubuntu在线安装mysql
1. sudo apt-get install mysql-server 2. apt-get isntall mysql-client 3. sudo apt-get install li ...
- $this->success传递数据
public function index_edit_doExport() { $search=$_POST['id']; $this->success(U('Tongji/index_edit ...
- 【Tomcat】tomcat设置http文件下载,配置文件下载目录
tomcat作为http的下载服务器,网上有很多办法 但我认为最简单的是:(亲测有效) 1.直接把文件放在 /var/lib/tomcat6/webapps/ROOT 目录下, 2.然后在网址中访问: ...
- C++中string.find()函数,string.find_first_of函数与string::npos
查找字符串a是否包含子串b,不是用strA.find(strB) > 0而是strA.find(strB) != string:nposstring::size_type pos = strA. ...
- python操作上级子文件
. └── folder ├── data │ └── data.txt └── test1 └── test2 └── test.py import os '***获取当前目录***'print o ...