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. Hakase and Nano 【思维博弈】

    Hakase and Nano 时间限制: 1 Sec  内存限制: 128 MB 提交: 400  解决: 104 [提交] [状态] [命题人:admin] 题目描述 Hakase and Nan ...

  2. 彻底地/ 终于地, 解决 关于apache 权限的问题了:: 修改 DocumentRoot后的 403错误: have no permission to access / on this server

    目录的权限都 应该设置 为 drwxr_xr_x, 即755, 而html下的文件的权限设置为; 644 即可! -x 只有目标文件对某些用户是可执行的或该目标文件是目录时才追加x 属性. -w权限, ...

  3. (转)PaperWeekly 第二十二期---Image Caption任务综述

    本文转自:http://mp.weixin.qq.com/s?__biz=MzIwMTc4ODE0Mw==&mid=2247484014&idx=1&sn=4a053986f5 ...

  4. Could not stop Cortex-M device! please check the JTAG cable的解决办法

      今天程序烧录后,进行调试时keil提示:Could not stop Cortex-M device!  please check the JTAG cable   如图: 于是我在网上搜了一下, ...

  5. Unity3D学习笔记(三十三):矩阵

    矩阵 矩阵就是一行和列组织起来的矩形数字块. 矩阵可以理解为是向量的数组.   矩阵的维度和记法 矩阵的维度是包含多少行多少列!例如1行2列的矩阵 记法:矩阵m中,对于第1行第2列的元素,我们记为m1 ...

  6. A successful Git branching model——经典篇

    A successful Git branching model In this post I present the development model that I’ve introduced f ...

  7. Python 编码规范 PEP8

    1 Introduction Guido 的核心思想是:对于代码而言,相比于写,它更多是被用来读的.这个指导旨在使Python代码更易读,且具有更强的协调性. 2 A Foolish Consiste ...

  8. Leetcode66-Plus One-Eassy

    Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The ...

  9. 20165306 实验二 Java面向对象程序设计

    实验二 Java面向对象程序设计 实验要求 1.提交最后三个JUnit测试用例(正常情况,错误情况,边界情况)都通过的截图,截图上要有画图加水印,输入自己的学号.本提交点考查JUnit会不会使用,测试 ...

  10. 改变input中的placeholder样式

    1.input[placeholder]{ color:#d5d5d5; } 2.input::-moz-placeholder { color: #d5d5d5; } input:-ms-input ...