http://acm.hdu.edu.cn/showproblem.php?pid=1241

对每个还未访问的点bfs,到达的点都标为一块,最后统计有多少块即可

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int maxn=101;
const int inf=0x3fffffff;
char maz[maxn][maxn];
int id[maxn][maxn];
int n,m,cnt; queue<int> que;
const int dx[8]={0,0,1,-1,1,1,-1,-1};
const int dy[8]={1,-1,0,0,1,-1,1,-1};
bool in(int x,int y){
return x>=0&&x<n&&y>=0&&y<m;
}
void bfs(int sx,int sy){
while(!que.empty())que.pop();
id[sx][sy]=++cnt;
que.push(sx*maxn+sy);
while(!que.empty()){
int x=que.front()/maxn,y=que.front()%maxn;que.pop();
for(int i=0;i<8;i++){
int tx=x+dx[i],ty=y+dy[i];
if(in(tx,ty)&&maz[tx][ty]!='*'&&id[tx][ty]==-1){
id[tx][ty]=id[x][y];
que.push(tx*maxn+ty);
}
}
}
} int main(){
while(scanf("%d%d",&n,&m)==2&&n){
cnt=0;
for(int i=0;i<n;i++){
scanf("%s",maz[i]);
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
id[i][j]=-1;
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(maz[i][j]!='*'&&id[i][j]==-1){
bfs(i,j);
}
}
} printf("%d\n",cnt);
}
return 0;
}

  

HDU 1241 Oil Deposits bfs 难度:0的更多相关文章

  1. HDU 1241 - Oil Deposits - [BFS]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 题意: 求某块平面上,连通块的数量.一个油田格子若周围八个方向也有一个油田格子,则认为两者相连通 ...

  2. HDU 1241 Oil Deposits(石油储藏)

    HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Probl ...

  3. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  4. HDOJ(HDU).1241 Oil Deposits(DFS)

    HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  5. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

  6. DFS(连通块) HDU 1241 Oil Deposits

    题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...

  7. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  8. HDU 1241 Oil Deposits (DFS/BFS)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  9. hdu 1241:Oil Deposits(DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

随机推荐

  1. 棋盘游戏---hdu1281(最大匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281   题目大意:就是车和车之间不能发生攻击.还有一部分位置不可以放置棋子.   解题思路:一行一列 ...

  2. uchome 常用函数示例

    一.inserttable //添加数据 //前3个参数 $tablename插入的表名称 $insertsqlarr数据数组 $returnid是否返回插入ID function inserttab ...

  3. HDU1452:Happy 2004(求因子和+分解质因子+逆元)上一题的简单版

    题目链接:传送门 题目要求:求S(2004^x)%29. 题目解析:因子和函数为乘性函数,所以首先质因子分解s(2004^x)=s(2^2*x)*s(3^x)*s(167^x); 因为2与29,166 ...

  4. c/c++ json使用

    比如出名的有CJson,c++一般用jsoncpp http://sourceforge.net/projects/jsoncpp/ jsoncpp:http://www.cnblogs.com/fe ...

  5. zoj3822

    这题说得是给了一个n*m的棋盘,每天在这个棋盘中放置一个棋子,不能放在之前已经摆放过得地方,求最后使得每行每列都有至少一个棋子的期望天数是多少,这样我们考虑怎么放,放哪里,显然数据大而且不知道状态怎么 ...

  6. spring AOP的注解实例

    上一篇写了spring AOP 的两种代理,这里开始AOP的实现了,个人喜欢用注解方式,原因是相对于XML方式注解方式更灵活,更强大,更可扩展.所以XML方式的AOP实现就被我抛弃了. 实现Sprin ...

  7. FlexPaper_1.2.1.swc——Flex在线显示PDF文档(使用FlexPaper)感悟

    http://www.cnblogs.com/wuhenke/archive/2010/03/16/1686885.html 想想自己先前搞PDF转SWF,然后在线浏览功能时,实在是费了不少精力.后来 ...

  8. python中小数点后取2位(四舍五入)以及取2位(四舍五不入)

    一.小数点后取2位(四舍五入)的方法方法一:round()函数其实这个方法不推荐大家使用,查询资料发现里面的坑其实很多,python2和python3里面的坑还不太一样,在此简单描述一下python3 ...

  9. Linux点亮一个灯

    一 文件及其驱动程序 1.解压linux 压缩包 使用命令: tar xzvf linux-3.0.8-20140925.tgz ( tar xvf ------.tar tar xzvf------ ...

  10. 【各类MQ比较】消息队列MQ

    目前业界有很多MQ产品,我们作如下对比: RabbitMQ 是使用Erlang编写的一个开源的消息队列,本身支持很多的协议:AMQP,XMPP, SMTP, STOMP,也正是如此,使的它变的非常重量 ...