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. java的LinkedList的用法

    http://blog.chinabyte.com/blog.php?do-showone-uid-135325-itemid-454704-type-blog.html 总结下,LinkedList ...

  2. Python Challenge 过关心得(1)

    正式开始第1关,这一关的URL的特殊部分是map. 这关的图片上有一个本子,上面写着K→M,O→Q,E→G,稍微思索就能发现这几个字母都是按照字母表的顺序向后移动了两位,那么最投机取巧的方法就是把ma ...

  3. Advanced Fruits(HDU 1503 LCS变形)

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

  4. jquery实现二级联动

    闲来没事,写点jquery练练手. <!--json代码部分 新建文件liandong.json--> var pron_city = { '省':['all'], '北京':[ {'市' ...

  5. 二分求解 三角形 stl的应用 涉及范围的二分查找可以先求上界再算下界,结果即上界减下界

     二分 Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu   Description You ...

  6. Java Service Wrapper

    Java Service Wrapper 将Java 应用程序部署成Windows系统服务Java Service Wrapper 1 Product Overview 1 Editions 2 Me ...

  7. myeclipse修改tomcat端口

    myeclipse自带的tomcat可以直接在myeclipse下面进行配置,但是如果是配置的tomcat 只能自己手动修改了

  8. J2EE之ANT

    Ant是一种基于Java的build工具.由一些内置任务组成.这些任务用于编译和运行java应用.创建文档.文件和文件夹的操作. 理论上来说.它有些类似于(Unix)C中的make ,但没有make的 ...

  9. 点滴记录——Centos 6.5 yum安装Ganglia

    转载请说明出处:http://blog.csdn.net/cywosp/article/details/39701141 注:下面操作都仅仅是在一台机器上操作 1. 安装php支持  yum inst ...

  10. char与byte的差别

    非常多刚開始学习的人(包含我,已经学了一年多java了)肯会对char和byte这两种数据类型有所疑惑,相互混淆,今天特地查了好多资料,对byte和char两种数据类型进行了总结和比較,先将结果与大家 ...