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. qt model/view 架构自定义模型之QStringListModel

    # -*- coding: utf-8 -*- # python:2.x #QStringListModel #QStringListModel 是最简单的模型类,具备向视图提供字符串数据的能力. # ...

  2. (史上最全的ios源码汇总)

    按钮类         按钮 Drop Down Control         http://www.apkbus.com/android-106661-1-1.html 按钮-Circular M ...

  3. sql执行顺序整理

    sql的执行顺序,是优化sql语句执行效率必须要掌握的.各个数据库可能有细小的差别,但大体顺序是相同的,这里只做大致说明. 一.总体执行顺序 在sql语句执行之前,还有SQL语句准备执行阶段,这里不做 ...

  4. [Cycle.js] From toy DOM Driver to real DOM Driver

    This lessons shows how we are able to easily swap our toy DOM Driver with the actual Cycle.js DOM Dr ...

  5. OLEDB简介

    OLE DB(OLEDB)是微软的战略性的通向不同的数据源的低级应用程序接口.OLE DB不仅包括微软资助的标准数据接口开放数据库连通性(ODBC)的结构化查询语言(SQL)能力,还具有面向其他非SQ ...

  6. Linux 下提高make的编译效率

    Linux下安装程序,一般都通过包管理器安装,但是包管理器或软件商店里的软件往往不是最新版本的,安装最新版软件时通常是下载源代码进行编译. 编译安装源代码时就离不开make了,但是make是单线程的, ...

  7. DotDensityRenderer

    关键之处在于获取每个点所代表的的值 这里使用geodatabase类库中idatastatistic接口进行统计字段,再将结果传递给esrisysytem.istatisticsResult进行. 需 ...

  8. mysql Fatal error encountered during command execution

    由于在操作中使用了自定义参数. 所以得在连接字符串中加上Allow User Variables=True: 表示允许用户自定义参数.

  9. 亲试,Windows平台上使用Qt5.2.1编写Android

    首先把工具都下载好: 1. Qt for Android: http://qt-project.org/downloads 2. Android NDK http://developer.androi ...

  10. 安卓初步:通讯技术介绍&&安卓介绍

    通讯技术: 1G    模拟制式    只能进行语音通话. 2G    GSM, CDMA    收发短信和邮件. 2.5G    GPRS, EDGE    访问wap网络数据.(图片, 壁纸, 文 ...