Red and Black(BFS or DFS) 分类: dfs bfs 2015-07-05 22:52 2人阅读 评论(0) 收藏
Description
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can’t move on red tiles, he can move only on black tiles.
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input
The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.
There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.
‘.’ - a black tile
‘#’ - a red tile
‘@’ - a man on a black tile(appears exactly once in a data set)
Output
For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Sample Input
6 9
….#.
…..#
……
……
……
……
……
@…
.#..#.
11 9
.#………
.#.#######.
.#.#…..#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#…….#.
.#########.
………..
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
.
…@…
.
..#.#..
..#.#..
0 0
Sample Output
45
59
6
13
BFS
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int w,h,sx,sy,cnt;
char map[30][30];
int vis[30][30];
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
struct Node
{
int x;
int y;
}Q[450];
Node s;
void bfs()
{
int front=0,rear=0;
Q[rear++]=s;
while(front<rear)
{
Node t=Q[front++];
for(int i=0;i<4;i++)
{
int x0=t.x+dx[i];
int y0=t.y+dy[i];
Node f;
f.x=x0;f.y=y0;
if(!vis[f.x][f.y]&&f.x>=0&&f.x<h&&f.y>=0&&f.y<w&&map[f.x][f.y]!='#')
{
vis[f.x][f.y]=1;
Q[rear++]=f;
if(map[f.x][f.y]=='.')
cnt++;
}
}
}
}
int main()
{
while(~scanf("%d%d",&w,&h))
{if(w==0||h==0)
break;
memset(vis,0,sizeof(vis));
memset(map,0,sizeof(map));
for(int i=0;i<h;i++)
{
scanf("%s",map[i]);
for(int j=0;j<w;j++)
if(map[i][j]=='@')
{
s.x=i;
s.y=j;
break;
}
}
cnt=0;
bfs();
printf("%d\n",cnt+1);}
return 0;
}
DFS
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int w,h,sx,sy,cnt;
char map[30][30];
int vis[30][30];
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
void dfs(int x,int y)
{
if(map[x][y]=='.')
cnt++;
if(x<0||x>=h||y<0||y>=w||map[x][y]=='#')
return;
for(int i=0;i<4;i++)
{
int x0=x+dx[i];
int y0=y+dy[i];
if(!vis[x0][y0])
{
vis[x0][y0]=1;
dfs(x0,y0);
}
}
}
int main()
{
while(~scanf("%d%d",&w,&h))
{if(w==0||h==0)
break;
memset(vis,0,sizeof(vis));
memset(map,0,sizeof(map));
for(int i=0;i<h;i++)
{
scanf("%s",map[i]);
for(int j=0;j<w;j++)
if(map[i][j]=='@')
{
sx=i;
sy=j;
break;
}
}
cnt=0;
dfs(sx,sy);
printf("%d\n",cnt+1);}
return 0;
}
//6 9
//....#.
//.....#
//......
//......
//......
//......
//......
//#@...#
//.#..#.
可以看出:写BFS时一般要有结构体来表示状态。
求最短路一般用BFS,其他的可能更多用的是DFS
两者的关键都在于找转态。
版权声明:本文为博主原创文章,未经博主允许不得转载。
Red and Black(BFS or DFS) 分类: dfs bfs 2015-07-05 22:52 2人阅读 评论(0) 收藏的更多相关文章
- 1.PHP站内搜索 分类: PHP开发实例 2015-07-31 22:48 4人阅读 评论(0) 收藏
PHP站内搜索:多关键字.加亮显示 1.SQL语句中的模糊查找 $sql = "SELECT * FROM `message` WHERE `content`like '%$k[0]%' a ...
- 修改android应用包名 分类: android 学习笔记 2015-07-16 22:48 4人阅读 评论(0) 收藏
由于项目需要,要修改已经开发好的应用包名,这本身很简单,但是如果你没找到门道,可能会白白浪费许多时间. 修改包名有三个地方要改,这三个地方的修改一定要按顺序来,否则你可能会遇到许多不必要的麻烦. 1. ...
- UI基础:视图控制器.屏幕旋转.MVC 分类: iOS学习-UI 2015-07-02 22:21 62人阅读 评论(0) 收藏
UIViewController 视图控制器,继承自UIResponder,作用:管理视图并且响应事件 功能: 1.分担APPdelegate的工作 2.实现模块独立,能提高复用性 创建UIViewC ...
- UI基础:UI程序执行顺序(UIApplicationMain()函数),自定义视图 分类: iOS学习-UI 2015-07-02 22:09 68人阅读 评论(0) 收藏
UI程序的一般执行顺序: 先进入main里面,执行函数UIApplicationMain(),通过该函数创建应用程序对象和指定其代理并实现监听,当执行函数UIApplicationMain()时还会做 ...
- Latex排版工具的使用(一) 分类: Latex 2014-06-14 22:52 448人阅读 评论(0) 收藏
使用Latex可以排版出漂亮的论文,尤其适合对含有数学公式论文的排版. 下面编写第一Latex源文件,实现对两个数学公式的排版: 新建文件first.tex: \documentclass{artic ...
- Mahout快速入门教程 分类: B10_计算机基础 2015-03-07 16:20 508人阅读 评论(0) 收藏
Mahout 是一个很强大的数据挖掘工具,是一个分布式机器学习算法的集合,包括:被称为Taste的分布式协同过滤的实现.分类.聚类等.Mahout最大的优点就是基于hadoop实现,把很多以前运行于单 ...
- HDU2212 DFS 2016-07-24 13:52 56人阅读 评论(0) 收藏
DFS Problem Description A DFS(digital factorial sum) number is found by summing the factorial of eve ...
- Eight(South Central USA 1998)(八数码) 分类: bfs 2015-07-05 22:34 1人阅读 评论(0) 收藏
The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...
- NYOJ-58 最小步数 AC 分类: NYOJ 2014-01-22 22:01 217人阅读 评论(0) 收藏
#include<stdio.h> void dfs(int step,int x,int y); int d[4][2] = {{1,0},{-1,0},{0,1},{0,-1}}; i ...
随机推荐
- Mysql 创建用户并对其赋予操作权限
授权命令GRANT 语句的语法如下: GRANT privileges (columns) ON what TO user IDENTIFIEDBY "password" WITH ...
- poj 1149 pigs(最大流)
题目大意:迈克在农场工作,农场有 m 个猪舍,每个猪舍有若干只猪,但是迈克不能打开任何一间猪舍.有 n 个顾客前来购买,每个顾客有最大的购买数量,每个顾客可以购买某些猪舍的猪,且顾客可以打开这些猪舍, ...
- Search for a Range 解答
Question Given a sorted array of integers, find the starting and ending position of a given target v ...
- 第06讲- DDMS中logcat的使用
1.DDMS使用 )Device选项卡 Device中罗列了Emulator中所有的进程,选项卡右上角那一排按钮分别为:调试进程.更新进程.更新进程堆栈信息.停止某个进程. )Threads选项卡 ...
- phpcms 去掉默认自动获取关键词、自动提取第一张图片?
进入后台,内容--模型管理--管理模型,选择文章模型的字段管理,选择第13项内容--修改,然后把字段提示代码中的2个checked去掉就行了. <label><input name= ...
- 解决getJdbcTemplate往oracle数据库中插入数据返回主键出错问题
我们使用Spring中的JdbcDaoSupport往Mysql中插入数据并返回主键代码,我们使用的mysql数据库,主键在数据库中设置为自增长:该类继承自JdbcDaoSupport,所以能直接使用 ...
- 基于ADODBX对数据库的CURD
学asp.net也有一个多星期了,之前对这个一无所知,也不知道怎么去找一些相关的资料去学习,不懂了就问问别人这个怎么做,那个怎么写,要不是有jsp和php的基础,估计还得弄上好长的时间来学习.记录一下 ...
- vue中关于computed的一点理解
computed相当于属性的一个实时计算,如果实时计算里关联了对象,那么当对象的某个值改变的时候,同事会出发实时计算. 例子: <body id="content"> ...
- Html5+css3实现3D转动效果
由于最近一直在忙着筹划去上海工作的事情,所以博客更新的速度也就慢了下来.前几天面试了几家公司,也拿到几份offer,总结了一些面试中遇到的问题,最近整理一下会和大家一起分享.今天呢,就和大家分享一下前 ...
- jquery.validate的使用
在页面上面引用 <script type="text/JavaScript" src="js/jQuery.js"></script> ...