HDU 1241 - Oil Deposits - [BFS]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241
题意:
求某块平面上,连通块的数量。一个油田格子若周围八个方向也有一个油田格子,则认为两者相连通。
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int maxn=;
const int dx[]={,,,-,,,-,-};
const int dy[]={,,-,,,-,,-}; int m,n;
char mp[maxn][maxn];
int vis[maxn][maxn];
queue<pii> Q;
inline bool in(pii o) {
return <=o.first && o.first<=m && <=o.second && o.second<=n;
}
void bfs(int x,int y)
{
while(!Q.empty()) Q.pop();
vis[x][y]=;
Q.push(make_pair(x,y));
while(!Q.empty())
{
pii now=Q.front(); Q.pop();
for(int k=;k<;k++)
{
pii nxt=make_pair(now.first+dx[k],now.second+dy[k]);
if(!in(nxt)) continue;
if(mp[nxt.first][nxt.second]=='*') continue;
if(vis[nxt.first][nxt.second]) continue;
Q.push(nxt);
vis[nxt.first][nxt.second]=;
}
}
} int main()
{
while(scanf("%d%d",&m,&n) && m*n>)
{
for(int i=;i<=m;i++) scanf("%s",mp[i]+);
memset(vis,,sizeof(vis));
int cnt=;
for(int i=;i<=m;i++)
{
for(int j=;j<=n;j++)
{
if(mp[i][j]=='@' && vis[i][j]==)
{
cnt++;
bfs(i,j);
}
}
}
cout<<cnt<<endl;
}
}
HDU 1241 - Oil Deposits - [BFS]的更多相关文章
- HDU 1241 Oil Deposits bfs 难度:0
http://acm.hdu.edu.cn/showproblem.php?pid=1241 对每个还未访问的点bfs,到达的点都标为一块,最后统计有多少块即可 #include <cstdio ...
- HDU 1241 Oil Deposits(石油储藏)
HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Probl ...
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
- hdu 1241 Oil Deposits(DFS求连通块)
HDU 1241 Oil Deposits L -DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & ...
- HDOJ(HDU).1241 Oil Deposits(DFS)
HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- DFS(连通块) HDU 1241 Oil Deposits
题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...
- HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU 1241 Oil Deposits (DFS/BFS)
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- HDU 1241 Oil Deposits 题解
Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
随机推荐
- REST API 调用 方法
METHOD DESCRIPTION GET Retrieves the specified resource POST Creates a resource ...
- 如何在VMware8虚拟机里安装Xp GHOST系统 解决不能启动Xp系统方法
好久没有装系统了.之前直接在硬盘中装,装个xp(c盘内).win7(d盘内).centos(虚拟机内)三系统同在一台笔记本电脑上.走了点弯路,这次记录下在虚拟机内装ghost xp. 安装步骤: 1. ...
- weak_ptr<T>智能指针
weak_ptr是为配合shared_ptr而引入的一种智能指针,它更像是shared_ptr的一个助手,而不是智能指针,因为它不具有普通指针的行为,没有重载operator*和operator-&g ...
- 【C++】C++中的流
目录结构: contents structure [-] 1.IO类 IO对象无拷贝状态 条件状态 文件流 文件模式 string流 1.IO类 除了istream和ostream之外,标准库还定义了 ...
- ld: library not found for -lstdc++.6
ld: library not found for -lstdc++.6 Xcode10 删除 libstdc++.6.tbd libstdc++.6.0.9.tbd 用 libc++.tbd lib ...
- AI 名校课程&书籍 需要学习
斯坦福李飞飞-深度学习计算机视觉 http://study.163.com/course/introduction/1003223001.htm 斯坦福李飞飞-深度学习计算机视觉---视频下载 htt ...
- 环境的配置; host 配置地址
1.环境的配置: C:\Windows\System32\drivers\etc 2.如果没有下载host 切记复制etc的文件,在c盘桌面打开,更改后替换 3.可下载host 和fiddle 4.多 ...
- Zookeeper —— 初识
什么是 Zookeeper Zookeeper 是一个开放源代码的分布式协调服务,由雅虎创建,是 Google Chubby 的开源实现: Zookeeper 是典型的分布式数据一致性的解决方案,分布 ...
- 通过动态SQL语句创建游标
DECLARE @sql varchar(100); DECLARE @TableName varchar(32); DECLARE @FieldName varchar(32); DECLARE @ ...
- nginx学习与使用
安装与运行 (从源码安装,这里OS为Ubuntu,参考资料:https://nginx.org/en/docs/configure.html) 1.下载并解压:https://nginx.org/en ...