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. 1.PHP站内搜索 分类: PHP开发实例 2015-07-31 22:48 4人阅读 评论(0) 收藏

    PHP站内搜索:多关键字.加亮显示 1.SQL语句中的模糊查找 $sql = "SELECT * FROM `message` WHERE `content`like '%$k[0]%' a ...

  2. 修改android应用包名 分类: android 学习笔记 2015-07-16 22:48 4人阅读 评论(0) 收藏

    由于项目需要,要修改已经开发好的应用包名,这本身很简单,但是如果你没找到门道,可能会白白浪费许多时间. 修改包名有三个地方要改,这三个地方的修改一定要按顺序来,否则你可能会遇到许多不必要的麻烦. 1. ...

  3. UI基础:视图控制器.屏幕旋转.MVC 分类: iOS学习-UI 2015-07-02 22:21 62人阅读 评论(0) 收藏

    UIViewController 视图控制器,继承自UIResponder,作用:管理视图并且响应事件 功能: 1.分担APPdelegate的工作 2.实现模块独立,能提高复用性 创建UIViewC ...

  4. UI基础:UI程序执行顺序(UIApplicationMain()函数),自定义视图 分类: iOS学习-UI 2015-07-02 22:09 68人阅读 评论(0) 收藏

    UI程序的一般执行顺序: 先进入main里面,执行函数UIApplicationMain(),通过该函数创建应用程序对象和指定其代理并实现监听,当执行函数UIApplicationMain()时还会做 ...

  5. Latex排版工具的使用(一) 分类: Latex 2014-06-14 22:52 448人阅读 评论(0) 收藏

    使用Latex可以排版出漂亮的论文,尤其适合对含有数学公式论文的排版. 下面编写第一Latex源文件,实现对两个数学公式的排版: 新建文件first.tex: \documentclass{artic ...

  6. Mahout快速入门教程 分类: B10_计算机基础 2015-03-07 16:20 508人阅读 评论(0) 收藏

    Mahout 是一个很强大的数据挖掘工具,是一个分布式机器学习算法的集合,包括:被称为Taste的分布式协同过滤的实现.分类.聚类等.Mahout最大的优点就是基于hadoop实现,把很多以前运行于单 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. DoTween学习笔记(一)

    DOTween是一个快速,高效,完全统一的类型安全的对象属性动画引擎,免费开源,大量的高级特性. DoTween兼容Unity4.5以上的版本,支持的平台: Win, Mac, Unity WebPl ...

  2. PC--CSS技巧

    1.图片不存在的时候,显示一个默认图片 <img src=”01.jpg” onerror=”this.src=’02.jpg'” /> 2.CSS强制图片自适应大小 img {width ...

  3. 详细解读Jquery各Ajax函数:$.get(),$.post(),$.ajax(),$.getJSON() —(转)

    一,$.get(url,[data],[callback]) 说明:url为请求地址,data为请求数据的列表(是可选的,也可以将要传的参数写在url里面),callback为请求成功后的回调函数,该 ...

  4. TCP/IP远程访问操作:rwho,rlogin,rcp和rsh

    TCP/IP网络通信 软件 包使用远程访问 的 命令 ,这些命令首先是由UC Berkely为Arpanet开发的.它允许您远程注册到另一个 系统 中,并从一个系统复制文件到另一个系统.您能取得关于一 ...

  5. MP3文件结构及解码概述

    MP3文件结构概述 Layer-3音频文件.MPEG(MovingPicture Experts Group)在汉语中译为活动图像专家组,特指活动影音压缩标准,MPEG音频文件是MPEG1标准中的声音 ...

  6. Unity 读取CSV与Excel

    前几天看到我们在游戏中需要动态加载某些角色的游戏策划值,关于这个问题怎么解决呢?其实办法很多种,归根到底,就是数据的读取.我们可以想到的存储数据的载体有很多.例如:txt,xml,csv,excel. ...

  7. XHTML 结构化:使用 XHTML 重构网站

    http://www.w3school.com.cn/xhtml/xhtml_structural_01.asp 我们曾经为本节撰写的标题是:"XHTML : 简单的规则,容易的方针.&qu ...

  8. CSS中文字体的英文名称(simsun)宋体,(Microsoft YaHei)微软雅黑

    CSS中文字体的英文名称(simsun)宋体,(Microsoft YaHei)微软雅黑 华文细黑:STHeiti Light [STXihei] 华文黑体:STHeiti 华文楷体:STKaiti ...

  9. HTML5简单入门系列(六)

    前言 之前几篇已经将HTML5的主要新增元素和特性简单介绍完毕,LZ一直在犹豫还要不要把其他元素也写出来,因为其实没什么东西可以写,就是自己用到时看一下就行.不过为了入门系列的完整,犹豫再三,还是决定 ...

  10. Oracle数据库之间数据同步

    这段时间负责某个项目开发的数据库管理工作,这个项目中开发库与测试数据库分离,其中某些系统表数据与基础资料数据经常需要进行同步,为方便完成指定数据表的同步操作,可以采用dblink与merge结合的方法 ...