POJ 1979 dfs和bfs两种解法
fengyun@fengyun-server:~/learn/acm/poj$ cat 1979.cpp
#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
#include<iterator>
#include<sstream>//istringstream
#include<cstring>
#include<queue>
using namespace std;
char map[21][21];
char visited[21][21];
int w, h;
int ans;
void dfs(int i, int j)
{
if(i<0||i>=h||j<0||j>=w)
return;
if(map[i][j]=='#')
return;
if(map[i][j]=='.'&&visited[i][j]==0)
{
ans++;
visited[i][j]=1;
dfs(i-1,j);
dfs(i+1,j);
dfs(i,j+1);
dfs(i,j-1);
}
}
int dir[4][2]={
{-1, 0},
{1, 0},
{0, 1},
{0, -1},
};
void bfs(int i, int j)
{
queue<int> q;
q.push(i*w+j);
visited[i][j]=1;
while(!q.empty())
{
//pop
//add neighbors
int ni,nj;
ni=q.front()/w;
nj=q.front()%w;
q.pop();
ans++;
for(int k=0;k<4;k++)
{
int row=ni+dir[k][0];
int col=nj+dir[k][1];
if(row>=0&&row<h&&col>=0&&col<w&&map[row][col]=='.'&&visited[row][col]==0)
{
visited[row][col]=1;
q.push(row*w+col);
}
}
}
}
int main()
{
while(cin>>w>>h && (w||h))
{
int x, y;
memset(visited, 0, sizeof(visited));
ans=0;
for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)
{
cin>>map[i][j];
if(map[i][j]=='@')
{
x=i;
y=j;
map[i][j]='.';
}
}
}
//dfs(x,y);
bfs(x,y);
cout<<ans<<endl;
}
return 0;
}
POJ 1979 dfs和bfs两种解法的更多相关文章
- HDU 2102 A计划 DFS与BFS两种写法 [搜索]
1.题意:一位公主被困在迷宫里,一位勇士前去营救,迷宫为两层,规模为N*M,迷宫入口为(0,0,0),公主的位置用'P'标记:迷宫内,'.'表示空地,'*'表示墙,特殊的,'#'表示时空传输机,走到这 ...
- Java描述表达式求值的两种解法:双栈结构和二叉树
Java描述表达式求值的两种解法:双栈结构和二叉树 原题大意:表达式求值 求一个非负整数四则混合运算且含嵌套括号表达式的值.如: # 输入: 1+2*(6/2)-4 # 输出: 3.0 数据保证: 保 ...
- 51nod 1165 整边直角三角形的数量(两种解法)
链接:http://www.51nod.com/Challenge/Problem.html#!#problemId=1165 直角三角形,三条边的长度都是整数.给出周长N,求符合条件的三角形数量. ...
- POJ 1157 LITTLE SHOP OF FLOWERS (超级经典dp,两种解法)
You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flo ...
- POJ 1182食物链(分集合以及加权两种解法) 种类并查集的经典
题目链接:http://icpc.njust.edu.cn/Problem/Pku/1182/ 题意:给出动物之间的关系,有几种询问方式,问是真话还是假话. 定义三种偏移关系: x->y 偏移量 ...
- Letter Combinations of a Phone Number:深度优先和广度优先两种解法
Letter Combinations of a Phone Number Given a digit string, return all possible letter combinations ...
- 【Java面试真题】剑指Offer53.2——0~n-1中缺失的数字(异或、二分两种解法)
[Java实现]剑指Offer53.2--0~n-1中缺失的数字:面试真题,两种思路分享 前面有另一道面试题[Java实现]剑指offer53.1--在排序数组中查找数字(LeetCode34:在排序 ...
- leetcode-91-解码方法(动态规划和递归两种解法)
题目描述: 一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数 ...
- Sort List[leetcode] 由归并排序的递归和循环,到本题的两种解法
归并排序能够有两种思路----top-down 和 bottom-up top-down: 递归实现,将数组分成两半.分别处理.再合并. 伪代码例如以下: split ( A[], l, r) { i ...
随机推荐
- Java [Leetcode 338]Counting Bits
题目描述: Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculat ...
- Linux Shell 脚本
1. 写一个脚本,利用循环计算10的阶乘#!/bin/shfactorial=1for a in `seq 1 10`do factorial=`expr $factorial \* $a ...
- OpenGL开发时,fatal error C1083: 无法打开包括文件:“gl\glut.h”: No such file or directory
本人使用的是vs2012,编写一个简单的opengl程序,运行的时候总是提示: fatal error C1083: 无法打开包括文件:“gl/glut.h”: No such file or dir ...
- [Everyday Mathematics]20150122
设 $f:[0,1]\to [0,1]$. (1). 若 $f$ 连续, 试证: $\exists\ \xi\in [0,1],\st f(\xi)=\xi$. (2). 若 $f$ 单调递增, 试证 ...
- hadoop2.20.0集群安装教程
一.安装的需要软件及集群描述 1.软件: Vmware9.0:虚拟机 Hadoop2.2.0:Apache官网原版稳定版本 JDK1.7.0_07:Oracle官网版本 Ubuntu12.04LTS: ...
- 给Webkit内核的浏览器控件增加互交功能
转载请说明出处,谢谢~~ 昨天封装了基于webkit的wke浏览器内核,做成了duilib的浏览器控件,实现了浏览功能,但是单单的浏览功能还不满足需求,在我的仿酷狗项目中乐库的功能需要与浏览器互交. ...
- ckeditor+jsp+spring配置图片上传
CKEditor用于富文本输入是极好的,它还有一些插件支持扩展功能,其中图片上传就是比较常用到的.本文简单记录我的实现步骤. 1.CKEditor除了提供三种标准版压缩包下载,还可根据自己的需求进行个 ...
- leetcode—3sum
1.题目描述 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...
- Javascript原理
1.javascript创建对象 创建新对象有两种不同的方法: 定义并创建对象的实例 person=new Object(); person.firstname="Bill"; p ...
- RabbitMQ (三) 发布/订阅 -摘自网络
这篇博客中,我们会做一些改变,就是把一个消息发给多个消费者,这种模式称之为发布/订阅(类似观察者模式). 为了验证这种模式,我们准备构建一个简单的日志系统.这个系统包含两类程序,一类程序发动日志,另一 ...