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. 不要怂,就是GAN (生成式对抗网络) (二)

    前面我们了解了 GAN 的原理,下面我们就来用 TensorFlow 搭建 GAN(严格说来是 DCGAN,如无特别说明,本系列文章所说的 GAN 均指 DCGAN),如前面所说,GAN 分为有约束条 ...

  2. [Codeforces Round #186 (Div. 2)] B. Ilya and Queries

    B. Ilya and Queries time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. s3c2416裸跑环境配置

    最近刚刚开始学习ARM-linux,上周买了块tq2416的板子,给的Linux资料太复杂太深奥不愿看,等不及想要把2416跑起来.于是到处找相关裸跑资料,可是用2416的人实在少,网上的资料更少,裸 ...

  4. TinyURL缩短网址服务 - Blog透视镜

    TinyURL是个缩短网址服务的网站,提供1个短网址转向指定到长网址,像是杂志书籍中若有网址太长,也都会用TinyURL来缩短网址,例如本篇文章:http://blog.openyu.org/2014 ...

  5. 【转】【Android】HAL分析

    原文网址:http://www.cnblogs.com/lcw/p/3335505.html HAL概述 以下是基于android4.0.3,对应其他低版本的代码,可能有所差异,但基本大同小异. An ...

  6. mysql 中文配置(转)

    Dos下连接mysql后,运行一下几项就可以插入中文了: SET character_set_client = gbk; SET character_set_connection = gbk; SET ...

  7. 杭州网赛 two rabbits (hdu 4745)

    算法很简单,问题是,怎么证明,答案是回文序列. 设a,b走的序列按顺时针是: a1 , a2 , a3 , ... , ak b1 , b2 , b3 , ... , bk 考虑端点的2种情况: 1. ...

  8. OpenWrt刷机

    http://blog.163.com/l1_jun/blog/static/14386388201441344612/ http://blog.chinaunix.net/uid-25073805- ...

  9. mysql C API 字符串玩转备份调优

  10. handsontable插件事件

    Hook插件 afterChange (changes: Array, source: String):1个或多个单元格的值被改变后调用     changes:是一个2维数组包含row,prop,o ...