题目大意:
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 HW and Hare 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

#include <bits/stdc++.h>
using namespace std;
int main()
{
int m[][];
m[][]=-; m[][]=;
m[][]=; m[][]=;
m[][]=; m[][]=-;
m[][]=; m[][]=; /// 上下左右四种位移 int h,w,flag[][];
while(~scanf("%d%d",&h,&w)&&h&&w)
{
int a,b,sum=; char ch[][];
for(int i=;i<=w;i++)
{
getchar();
for(int j=;j<=h;j++)
{
scanf("%c",&ch[i][j]);
if(ch[i][j]=='@')
a=i,b=j; /// 记录人的位置,即起点
}
} memset(flag,,sizeof(flag));
flag[a][b]=; queue <int> x,y;
x.push(a),y.push(b);
while(!x.empty()&&!y.empty())
{
for(int i=;i<;i++)
{
a=x.front()+m[i][];
b=y.front()+m[i][];
if(a>&&a<=w&&b>&&b<=h //如果点在范围内,且
&&ch[a][b]!='#'&&!flag[a][b])//位于没走过的黑瓷砖
x.push(a), y.push(b), sum++; //放入队列 否则忽略
flag[a][b]=;
}
x.pop(), y.pop();
}
printf("%d\n",sum);
} return ;
}

Red and Black 模板题 /// BFS oj22063的更多相关文章

  1. Pathfinding 模板题 /// BFS oj21413

    题目大意: Description Bessie is stranded on a deserted arctic island and wants to determine all the path ...

  2. POJ:Dungeon Master(三维bfs模板题)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 D ...

  3. POJ-2251 Dungeon Master (BFS模板题)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  4. hdu1242 又又又是逃离迷宫(bfs模板题)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1242/ 这次的迷宫是有守卫的,杀死一个守卫需要花费1个单位的时间,所以以走的步数为深度,在每一层进行搜索,由于走 ...

  5. 用一道模板题理解多源广度优先搜索(bfs)

    题目: //多元广度优先搜索(bfs)模板题详细注释题解(c++)class Solution { int cnt; //新鲜橘子个数 int dis[10][10]; //距离 int dir_x[ ...

  6. HDU-3549 最大流模板题

    1.HDU-3549   Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://ww ...

  7. HDU 4280:Island Transport(ISAP模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...

  8. AC自动机 - 多模式串匹配问题的基本运用 + 模板题 --- HDU 2222

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. POJ 1459 Power Network(网络最大流,dinic算法模板题)

    题意:给出n,np,nc,m,n为节点数,np为发电站数,nc为用电厂数,m为边的个数.      接下来给出m个数据(u,v)z,表示w(u,v)允许传输的最大电力为z:np个数据(u)z,表示发电 ...

随机推荐

  1. 戏说 .NET GDI+系列学习教程(三、Graphics类的应用_验证码扩展)

    从别人那拷贝的 #region 定义和初始化配置字段 //用户存取验证码字符串 public string validationCode = String.Empty; //生成的验证码字符串 pub ...

  2. jQuery 实现复选框全选、反选及获取选中的值

    实现复选框全选,反选及获取选中的值: 代码如下: <!doctype html> <html lang="en"> <head> <met ...

  3. 剑指offer——49礼物的最大价值

    题目描述 在一个m*n的棋盘的每一格都放有一个礼物,每个礼物都有一定的价值(价值大于0).你可以从棋盘的左上角开始拿格子里的礼物,并每次向左或者向下移动一格,知道到达棋盘的右下角.给定一个棋盘及其上面 ...

  4. <Linux>Linux基础学习(兄弟连版本)

    1.Linux系统简介 1.1 Unix与Linux发展史 父子关系:Unix 是Linux的前身 1969年,肯丶汤姆森开发Unix系统(为了加快玩游戏的速度 - -,自己开发的系统) 1971年, ...

  5. jmeter-测试webservice接口

    测试webservice接口(soap类型接口) 一.webservice协议的本质 一个经过封装的post类型的HTTP请求 Web service一般就是用SOAP协议通过HTTP来调用它,其实他 ...

  6. 本地调试H5页面

    摘要 详细讲述微信H5页面调试(安装在安卓或iOS手机上的),钉钉内H5页面的调试,QQ.微博以及各浏览器上H5页面的调试方法 背景 大学毕业快要一年了,用leader的话说我也是有一年开发经验的前端 ...

  7. C#异步编程----async和await组合的写法

    微软示例: private async void StartButton_Click(object sender, RoutedEventArgs e) { // ExampleMethodAsync ...

  8. mimikaz获取明文密码

    一.windows2008以上不保存明文密码解决办法 mimikatz.exe "privilege::debug" "sekurlsa::logonpasswords& ...

  9. Spring mvc Hello World

    Spring mvc Hello World 添加依赖 <dependency> <groupId>org.springframework</groupId> &l ...

  10. k8s 命令

    Kubernetes 常用安装和使用命令总结 获取所有命名空间上的pod kubectl get pod -o wide --all-namespaces 查看pod IP时使用 kubectl ge ...