HDU 1241.Oil Deposits-求连通块DFS or BFS
Oil Deposits
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 43487 Accepted Submission(s): 25275
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
1
2
2
#include<bits/stdc++.h>
using namespace std;
const int N=+;
char a[N][N];
int dis[][]={{,},{-,},{,},{,-},{,},{-,},{,-},{-,-}};
int n,m,num;
void DFS(int x,int y){
int xx,yy;
for(int i=;i<;i++){
xx=dis[i][]+x;
yy=dis[i][]+y;
if(xx>=&&yy>=&&xx<n&&yy<m){
if(a[xx][yy]=='@'){
a[xx][yy]='*';
DFS(xx,yy);
}
}
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
memset(a,,sizeof(a));
if(n==&&m==)break;
num=;
for(int i=;i<n;i++){
for(int j=;j<m;j++)
cin>>a[i][j];
}
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(a[i][j]=='@'){
num++;
a[i][j]='*';
DFS(i,j);
}
}
}
printf("%d\n",num);
}
return ;
}
代码(BFS):
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int N=+;
char mat[N][N];
int dir[][]={{,},{-,},{,},{,-},{,},{-,},{,-},{-,-}};
int n,m,sum;
struct Node{
int x,y;
};
void BFS(int x,int y){
queue<Node>q;
Node node;
node.x=x;
node.y=y;
q.push(node);
while(!q.empty()){
Node cur=q.front();
Node next;
q.pop();
for(int i=;i<;i++){
next.x=cur.x+dir[i][];
next.y=cur.y+dir[i][];
if(mat[next.x][next.y]=='@'){
mat[next.x][next.y]='*';
q.push(next);
}
}
}
}
int main(){
while(scanf("%d%d",&n,&m)){
if(n==&m==)break;
memset(mat,,sizeof(mat));
sum=;
int cur=;
for(int i=;i<=n;i++)
scanf("%s",mat[i]+);
for(int i=;i<=n;i++){
for(int j=;j<=m;j++){
if(mat[i][j]=='@'){
sum++;
mat[i][j]='*';
BFS(i,j);
}
}
}
printf("%d\n",sum);
}
return ;
}
HDU 1241.Oil Deposits-求连通块DFS or BFS的更多相关文章
- hdu 1241 Oil Deposits(DFS求连通块)
HDU 1241 Oil Deposits L -DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & ...
- DFS(连通块) HDU 1241 Oil Deposits
题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...
- HDU 1241 Oil Deposits --- 入门DFS
HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...
- HDOJ(HDU).1241 Oil Deposits(DFS)
HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- HDU 1241 Oil Deposits(石油储藏)
HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Probl ...
- HDU 1241 - Oil Deposits - [BFS]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 题意: 求某块平面上,连通块的数量.一个油田格子若周围八个方向也有一个油田格子,则认为两者相连通 ...
- HDU 1241 Oil Deposits (DFS)
题目链接:Oil Deposits 解析:问有多少个"@"块.当中每一个块内的各个"@"至少通过八个方向之中的一个相邻. 直接从"@"的地方 ...
- HDU 1241 Oil Deposits (DFS or BFS)
链接 : Here! 思路 : 搜索判断连通块个数, 所以 $DFS$ 或则 $BFS$ 都行喽...., 首先记录一下整个地图中所有$Oil$的个数, 然后遍历整个地图, 从油田开始搜索它所能连通多 ...
- HDU 1241 Oil Deposits【DFS】
解题思路:第一道DFS的题目--- 参看了紫书和网上的题解-- 在找到一块油田@的时候,往它的八个方向找,直到在能找到的范围内没有油田结束这次搜索 可以模拟一次DFS,比如说样例 在i=0,j=1时, ...
随机推荐
- 虚拟机中配置SQL SERVER2008R2远程访问
VM虚拟机中配置数据库访问 选择虚拟机设置--硬件--网络适配器,选择桥接模式:直接连接物理网络 不可选用主机模式(与主机共享专用网络) 数据库远程配置,转自:http://jingyan.baidu ...
- Docker从零到实践过程中的坑
欢迎指正: Centos7 下的ulimit在Docker中的坑 http://www.dockone.io/article/522 僵尸容器:Docker 中的孤儿进程 https://yq.ali ...
- Hibernate中1+N问题以及解决方法
1. Hibernate中的1+N问题描述 在多对一关系中,当我们需要查询多的一方对应的表的记录时,可以用一条sql语句就能完成操作.然而,在多的一方的实体类中的@ManyToOne标注的fetch的 ...
- RCP 项目启动图片设置
第一步 选择启动图片命名为 splash.bmp 第二步 添加 扩展点 然 后在右边的扩展元素细节中填入相应的信息,比如我们在这里的application属 性 为 org.vwpolo.rcp.ex ...
- iview框架 两侧弹框 出现第二层弹框 一闪而过的问题
分析原因:寡人怀疑可能是,两层弹出框 采用的是一个开关值,发生了覆盖 解决方式 是在第二层弹框外套层计时器 源代码如下: 修改后为:
- WPF控件开发(2) 自动完成(AutoComplete)-1
自动完成功能使用范围很广,多以TextBox或ComboBox的形式出现,在输入的同时给予候选词,候选词一般有两种方式获取. 一种类似Baidu,Google,Bing之类的搜索引擎所用的直接给予前十 ...
- ECMAScript5.1
http://lzw.me/pages/ecmascript/ ECMAScript5.1中文版 https://msdn.microsoft.com/zh-cn/library/dn656907. ...
- 【Maximum Subarray 】cpp
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- 非旋Treap总结 : 快过Splay 好用过传统Treap
非旋$Treap$ 其高级名字叫$Fhq\ Treap$,既然叫$Treap$,它一定满足了$Treap$的性质(虽然可能来看这篇的人一定知道$Treap$,但我还是多说几句:$Fhp\ Treap$ ...
- 聊聊、Tomcat中文乱码和JVM设置
set JAVA_OPTS=%JAVA_OPTS% -server -Xms512m -Xmx512m -Dfile.encoding=GBK -Dsun.jnu.encoding=GBK