题目大意:
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. js中的函数声明和函数表达式的区别

    目录 一.声明与表达式的格式 1.1 声明式的格式: 1.2 表达式的格式: 二.区别 2.1 函数表达式可以直接在后面加括号执行,而函数声明不可以. 2.2 函数表达式可以被提前解析出来 2.3 命 ...

  2. 20、formAdd,javascript实现动态添加

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. 服务器安装TeamViewer 13

    服务器安装TeamViewer 13 服务器上安装TeamViewer,网上找了个教程开始安装,里面有坑,安装时要根据自己的情况而定.Linux系统更新太快,网上教程可能会有一些出入. TeamVie ...

  4. Jeecg中通过Spring_AOP+注解方式实现日志的管理

    转载;https://blog.csdn.net/ma451152002/article/details/77234236 Jeecg中通过Spring_AOP+注解方式实现日志的管理 一.设计思路 ...

  5. 常用内置模块(三)——shelve、xml、hashlib、configparser

    常用模块(三) 一.shelve模块 1.shelve模块也是一种序列化模块,内部使用的是pickle模块,所以也存在跨平台性差的问题 2.特点: 只要提供一个文件名即可 读写的方式和字典一样 将数据 ...

  6. Python的一些列表方法

    1.append:方法append用于将一个对象附加到列表末尾,直接修改列表 lst=[1,2,3,4] lst.append(5) print(lst) 1,2,3,4,5 2.clear:方法cl ...

  7. react-router v4 理解

    1.Router (1)最基础的路由器,必须有history属性 (2)BrowserRouter和HashRouter都是由Router组件扩展而来 2.BrowserRouter (1)Brows ...

  8. vue 学习 cli3常用配置

    ---恢复内容开始--- cli3以后,构建的项目更加的简洁,配置文件也没有向cli2那样暴漏出来,但这并不代表cli3是不可配置的,我们只需要在根目录下添加一个vue.config.js作为项目的配 ...

  9. loj2000[SDOI2017]数字表格

    题意:f为Fibnacci数列.求$\prod_{1<=i<=n,1<=j<=m} f[gcd(i,j)]$. n,m<=1e6. 标程: #include<bit ...

  10. 47. List中特有的方法

    集合的体系:--------------| Collection  单列集合的根接口----------| List 如果实现了List接口的集合类,该类具备的特点是:有序,可重复---------- ...