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. 在JasperReport中填充JavaBean(4)

    使用Parameters参数对象传递字符串的示例,本节将演示打印List接口中Userinfo.java实体类的示例,打印的数据源不是来自于Parameters对象,而是JRBeanCollectio ...

  2. [C入门 - 游戏编程系列] 贪吃蛇篇(三) - 蛇定义

    蛇是这个游戏的主角,要实现的功能也是最复杂的一个.因为蛇不止有属性,还有行为.它会动,还会吃东西,还会长大!而且还会死!这是很要命的.我一向看不懂复杂的代码,也写不出复杂的代码.所以对于蛇,我很纠结, ...

  3. 纯js实现div内图片自适应大小

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. AsEnumrable和AsQueryable的区别

    http://www.cnblogs.com/jianglan/archive/2011/08/11/2135023.html 在写LINQ语句的时候,往往会看到.AsEnumerable() 和 . ...

  5. ORA-19573: cannot obtain exclusive enqueue for datafile 1

    还原Oracle数据库时出现ORA-19870和ORA-19573错误,如: RMAN> restore database; Starting restore at 11-DEC-12 usin ...

  6. Effective Java实作hashCode() - 就是爱Java

    hashCode()这个方法,也是定义在Object class中,这个是所有class的base class,因此所有的class也都继承这个方法,预设是传回这个对象储存的内存地址编号,因为Mix覆 ...

  7. C# 文件夹操作

    追加文件    StreamWriter sw = File.AppendText(Server.MapPath(] != Path.DirectorySeparatorChar)         a ...

  8. Android基础之退出应用程序Demo

    对于Android我也不是很熟悉,只是学习一些基本内容就OK.所以写的内容也很简单.本Demo要实现的效果就是双击返回键弹出提示框确认是否退出程序. 一.废话少说直接上代码.至于涉及到的相关包在Ecl ...

  9. setOpaque(true);设置控件不透明

    setOpaque(true);设置控件不透明setOpaque(false);设置控件透明

  10. MSDN中HttpWebRequest/HttpWebResponse用法

    HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://home.cnblogs.com/u/weiweiboqi/ ...