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. Socket,非阻塞,fcntl

    一.fcntl 用以下方法将socket设置成为非阻塞方式 int  flags = fcntl(socket,F_GETFL,0); fcntl(socket,F_SETFL,flags|O_NON ...

  2. JavaScript 基础一

    内部: <Script Language="JavaScript" type="text/javascript"> JavaScript代码 < ...

  3. Ubuntu的防火墙UFW

    这是个简单的防火墙,可以直接在命令行启停,也可安装提图形端gufw *安装 sudo apt-get install ufw gufw *常用命令 sudo ufw enable //启动 ufw d ...

  4. kaggle之泰坦尼克的沉没

    Titanic 沉没 参见:https://github.com/lijingpeng/kaggle 这是一个分类任务,特征包含离散特征和连续特征,数据如下:Kaggle地址.目标是根据数据特征预测一 ...

  5. 纯html网页重定向与跳转

    javaScript 跳转 方法一: <script language="javascript">    window.location = "http:// ...

  6. Autofac创建实例的方法总结 【转】

    Autofac创建实例的方法总结   1.InstancePerDependency 对每一个依赖或每一次调用创建一个新的唯一的实例.这也是默认的创建实例的方式. 官方文档解释:Configure t ...

  7. MJExtension(JSON到数据模型的自动转换)

    整理自:http://www.jianshu.com/p/93c242452b9b. 1.MJExtension的功能 字典-->模型 模型-->字典 字典数组-->模型数组 模型数 ...

  8. No1_4.数组的基本操作_Java学习笔记

    import java.util.Arrays; public class HelloArrayOp { public static void main(String[] args) { // TOD ...

  9. Javascript经典实例 - 字符串

    1] 'this is a string'这是字符串直接量,new String('this is a string')这是字符串对象,字符串对象可以用字符串对象所带的属性和方法,直接量在“表面上”也 ...

  10. 远程复制 scp命令

    定义 本机为A,用户名为usera,登录远程主机B的为userb,IP为remote_ip 1. 从B 拷贝文件到A机器  用下面的命令 scp userb@remote_ip:remote_path ...