Red and Black

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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) 

 

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<cstdio>
#include<cstring> using namespace std;
int n,m,sx,sy;
char mp[][];
int vis[][];
int dx[]={,,,-};
int dy[]={,-,,};
bool check(int x,int y)
{
return x>=&&x<n&&y>=&&y<m;
}
struct node
{
int x,y;
}St[]; int bfs()//手写的队列,队列的尾端就是到达的点的数量
{
memset(vis,,sizeof vis);
int st=,en=;
vis[St[].x][St[].y]=;
while(st<en)
{
node e=St[st++];
for(int i=;i<;i++)
{
node w=e;
w.x=e.x+dx[i],w.y=e.y+dy[i];
if(check(w.x,w.y)&&vis[w.x][w.y]==&&mp[w.x][w.y]!='#')
{
vis[w.x][w.y]=;
St[en++]=w;
}
}
}
return en;
} int main()
{
while(scanf("%d%d",&m,&n)!=EOF&&(n||m))
{
for(int i=;i<n;i++)
{
scanf("%s",mp[i]);
for(int j=;j<m;j++)
if(mp[i][j]=='@')
St[].x=i,St[].y=j;
}
printf("%d\n",bfs());
}
return ;
}

Red and Black

HDU 1312 Red and Black(bfs)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

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

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

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

    传送门: 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)

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

  7. hdu 1312:Red and Black(DFS搜索,入门题)

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

  8. 题解报告:hdu 1312 Red and Black(简单dfs)

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

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

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

随机推荐

  1. h5 如何打包apk

    1.需要下载安装MyEclipse2014,Android SDK,eclipse(需配置Android开发环境) Java和Android环境安装与配置. 2.打开MyEclipse2014,新建一 ...

  2. linux查找命令find

    -1 linux的查找命令有两个: locate find locate:有一个索引库,故速度快,但是新加入的一般不再索引库中,故可能无法查到 find:搜索速度慢,但是功能及其强大,可以追加命令动作 ...

  3. linux下mysql忘记root密码的解决方案

    1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 状态下,其他的用户也可以任意地登录 ...

  4. jdbc 处理mysql procedure返回的多个结果集

    1:测试数据库表user mysql> desc user$$ +-------+-------------+------+-----+---------+----------------+ | ...

  5. QTabWidget and QTabBar.的文字的颜色设置,三种方法

    see the code after subclassingTabWidget::TabWidget(QWidget *parent): QTabWidget(parent),mousePressFl ...

  6. Qt5中生成和使用静态库

    在QT中静态库的后缀名为.a,在vs中开发的静态库后缀名为.lib.QT版本为5.2.1,系统为Windows. 一. 静态库的生成 新建项目. 新建一个静态库的项目,如图1.1所示:项目名称为tes ...

  7. Eclipse上GIT插件EGIT使用手册ᄃ

    Eclipse上GIT插件EGIT使用手册 一_安装EGIT插件 http://download.eclipse.org/egit/updates/ 或者使用Eclipse Marketplace,搜 ...

  8. Linux系统编程(14)——shell常用命令

    1. ls命令 ls命令是列出目录内容(ListDirectory Contents)的意思.运行它就是列出文件夹里的内容,可能是文件也可能是文件夹. "ls -l"命令已详情模式 ...

  9. Combinations 解答

    Question Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. F ...

  10. hdu 1987-How many ways(dp)

    解析:假设机器人在(x,y)这个点,能量为power,那么可以到达它右下角曼哈顿距离小于等于power的地方,再以该点为起点继续搜索. 代码如下: #include<cstdio> #in ...