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.

InputThe 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) 
OutputFor 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 思路:BFS
AC Code:
#include<iostream>
#include<cstdio>
#include<queue>
#include<utility>
#include<cstring>
using namespace std;
typedef pair<int,int> P;
char maps[][];
int vis[][];
int dx[]={,,-,},dy[]={,,,-};
int W,H,cnt;
void bfs(P p){
queue<P> q;q.push(p);
while(!q.empty() ){
p=q.front() ;q.pop() ;
int x=p.first,y=p.second;
for(int i=;i<;i++)
{
int nx=x+dx[i],ny=y+dy[i];
if(nx>=&&nx<H&&ny>=&&ny<W&&maps[nx][ny]!='#'&&vis[nx][ny]==){
cnt++;
vis[nx][ny]=;
q.push(P(nx,ny));
}
}
} }
int main(){
while(~scanf("%d%d",&W,&H)){
if(W==&&H==) break;
int x,y;
getchar();
memset(vis,,sizeof(vis));
for(int i=;i<H;i++){
for(int j=;j<W;j++){
scanf("%c",&maps[i][j]);
if(maps[i][j]=='@') { x=i; y=j; }
}
getchar();
}
cnt=;vis[x][y]=;
bfs(P(x,y));
printf("%d\n",cnt);
}
}

Red and Black HDU - 1312的更多相关文章

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

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

  2. HDU 1312 Red and Black --- 入门搜索 DFS解法

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

  3. HDU 1312:Red and Black(DFS搜索)

      HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  4. HDU 1312 Red and Black(bfs,dfs均可,个人倾向bfs)

    题目代号:HDU 1312 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/100 ...

  5. HDU 1312 Red and Black (dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

  6. HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)

    Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  7. HDU 1312 Red and Black(最简单也是最经典的搜索)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

  8. HDU 1312 Red and Black(经典DFS)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 一道很经典的dfs,设置上下左右四个方向,读入时记下起点,然后跑dfs即可...最后答 ...

  9. HDU 1312 Red and Black (DFS & BFS)

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:有一间矩形房屋,地上铺了红.黑两种颜色的方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相 ...

随机推荐

  1. linux内核中的vgaarb是什么?

    答: vga仲裁器(vga arbiter),是内核中的一个模块

  2. SpringBoot 使用Mybatis-Plus

    简介 Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发.提高效率而生. 特性 无侵入:Mybatis-Plus 在 My ...

  3. 【做题】CF239E. k-d-sequence——线段树

    首先,容易得到判断一个子串为"good k-d sequence"的方法: 子串中没有重复元素,且所有元素模d相等. 记mx为除以d的最大值,mn为除以d的最小值,则\(mx-mn ...

  4. DAG最小路径点覆盖

    Problem 给出一个有向无环图 (\(DAG\)),求出最少使用其中多少条互不相交的路径覆盖所有点. Solution 若有 \(n\) 个点,对于每个点 \(i\) ,我们将它拆成两个点 \(i ...

  5. jQuery实现Marquee

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

  6. 【Python】【有趣的模块】【Requests】session & cookie

    保存http请求的状态(请求的上下文) [区别&联系] 1. cookie保存在客户端的浏览器,比如标识是哪个请求者.购物车应用等 session保存在服务端,http连接时无则创建,有则用现 ...

  7. 利用angularjs完成注册表单

    ng-init="username = 'first'"设置初始显示first字段 ng-class="{'error':signUpForm.username.$inv ...

  8. 【BZOJ】1830: [AHOI2008]Y型项链

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1830 直接枚举目标串是什么,目标串一定是三个字符串的某一个前缀(注意可能为空),然后判断一 ...

  9. zipkin启动报错(Caused by: java.lang.ClassNotFoundException: zipkin.Component)的解决方法

    使用ziplin依赖: <dependency> <groupId>org.springframework.cloud</groupId> <artifact ...

  10. Lua和C++交互 学习记录之七:C++全局函数注册为Lua模块

    主要内容转载自:子龙山人博客(强烈建议去子龙山人博客完全学习一遍) 部分内容查阅自:<Lua 5.3  参考手册>中文版 译者 云风 制作 Kavcc vs2013+lua-5.3.3 1 ...