Red and Black
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 27891   Accepted: 15142

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) 
The end of the input is indicated by a line consisting of two zeros. 

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

Source

 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
char map[][];
bool vis[][];
int dx[]={,,,-},dy[]={,-,,};
int w,h;
int sx,sy;
struct node
{
int x,y;
};
int bfs()
{
queue<node> q;
int ans=;
memset(vis,,sizeof(vis));
node s,temp;
s.x=sx,s.y=sy;
q.push(s);
while(!q.empty())
{
s=q.front();
q.pop();
ans++;
for(int i=;i<;i++)
{
temp.x=s.x+dx[i];
temp.y=s.y+dy[i];
if(temp.x>=&&temp.x<h&&temp.y>=&&temp.y<w&&vis[temp.x][temp.y]==&&map[temp.x][temp.y]=='.')
{ vis[temp.x][temp.y]=;
q.push(temp); }
}
}
return ans;
}
int main()
{
int i,j;
freopen("in.txt","r",stdin);
while(scanf("%d%d",&w,&h))
{
if(w==&&h==)
break;
for(i=;i<h;i++)
scanf("%s",map[i]);
for(i=;i<h;i++)
for(j=;j<w;j++)
if(map[i][j]=='@')
{
sx=i,sy=j;
break;
} cout<<bfs()<<endl;
}
return ;
}

Red and Black(poj 1979 bfs)的更多相关文章

  1. DFS:Red and Black(POJ 1979)

    红与黑 题目大意:一个人在一个矩形的房子里,可以走黑色区域,不可以走红色区域,从某一个点出发,他最多能走到多少个房间? 不多说,DFS深搜即可,水题 注意一下不要把行和列搞错就好了,我就是那样弄错过一 ...

  2. POJ 1979 Red and Black (红与黑)

    POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a ...

  3. HDU 1312 Red and Black --- 入门搜索 BFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  4. POJ 1979 Red and Black (BFS)

    链接 : Here! 思路 : 简单的搜索, 直接广搜就ok了. /****************************************************************** ...

  5. POJ 1979 dfs和bfs两种解法

      fengyun@fengyun-server:~/learn/acm/poj$ cat 1979.cpp #include<cstdio> #include<iostream&g ...

  6. OpenJudge/Poj 1979 Red and Black / OpenJudge 2816 红与黑

    1.链接地址: http://bailian.openjudge.cn/practice/1979 http://poj.org/problem?id=1979 2.题目: 总时间限制: 1000ms ...

  7. poj 1979 Red and Black 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=1979 Description There is a rectangular room, covered with square tiles ...

  8. POJ 1979 Red and Black dfs 难度:0

    http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; ...

  9. poj 1979 Red and Black(dfs)

    题目链接:http://poj.org/problem?id=1979 思路分析:使用DFS解决,与迷宫问题相似:迷宫由于搜索方向只往左或右一个方向,往上或下一个方向,不会出现重复搜索: 在该问题中往 ...

随机推荐

  1. Scala学习笔记--提取器unapply

    提取器就是一个带有unapply方法的对象.你可以把unapply方法当做是伴生对象中apply方法的反向操作. apply方法接收构造参数,然后将他们变成对象. 而unapply方法接受一个对象,然 ...

  2. Array.prototype.slice()的用法

    我们知道,Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js ...

  3. Android 代码编辑器中实现代码语法高亮

    想写一款Android手机上的代码编辑器,实现类似c4droid中代码语法高亮 通过Android中的控件WebView中嵌入html网页,html引入CodeMirror这个第三方库就可以了,其实就 ...

  4. final关键字的作用

    final 可以修饰类.方法.变量. (1):final修饰的变量是一个常量,只能被赋值一次. 常量一般使用final声明,如: public static final String CHINA = ...

  5. HDU 4009 Transfer water

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4009 题意:给出一个村庄(x,y,z).每个村庄可以挖井或者修建水渠从其他村庄得到水.挖井有一个代价, ...

  6. SGU 294 He's Circles

    题意:一个项链有n个珠子,每个珠子为黑色或白色.问有多少种不同的项链? 注意,n的数量十分大,因此,我们枚举i(1<=i<=n),令L=n/i,求出L的欧拉函数,则这些数和L互质,因此gc ...

  7. PowerShell 管道和对象成员

    2.1  管道 在各种现代的shell中,均支持管道的概念. 管道的最大特点就是:  前一个命令的输出作为后一个命令的输入.cmd.bash均支持管道的概念,这里我就不多说了,下面我们说说PS中 管道 ...

  8. MySQL脚本

    一. move.sql use items set @endTime=(select DATE_SUB(now(),INTERVAL 3 MONTH)); REPLACE INTO new_items ...

  9. inux关于readlink函数获取运行路径的小程序

    inux关于readlink函数获取运行路径的小程序   相关函数: stat, lstat, symlink 表头文件: #include <unistd.h> 定义函数:int  re ...

  10. ios相册

    1, 系统图片剪裁的问题 http://www.cnblogs.com/liulunet/archive/2013/01/19/2866399.html