Red and Black HDU - 1312
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的更多相关文章
- HDU 1312 Red and Black --- 入门搜索 BFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- HDU 1312 Red and Black --- 入门搜索 DFS解法
HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...
- HDU 1312:Red and Black(DFS搜索)
HDU 1312:Red and Black Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & ...
- 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 ...
- 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 ...
- 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 ...
- HDU 1312 Red and Black(最简单也是最经典的搜索)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...
- HDU 1312 Red and Black(经典DFS)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 一道很经典的dfs,设置上下左右四个方向,读入时记下起点,然后跑dfs即可...最后答 ...
- HDU 1312 Red and Black (DFS & BFS)
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:有一间矩形房屋,地上铺了红.黑两种颜色的方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相 ...
随机推荐
- 真心觉得没有必要使用dock和kde桌面
在安装了, 并使用cairo-dock后, 如果不使用 cairo-dock(fallback mode) 或不启用硬件加速, 将导致 cpu的使用率一直是100%, 而且风扇响的太厉害. 说明doc ...
- SpringBoot JDBC 源码分析之——NamedParameterJdbcTemplate 查询数据返回bean对象
1,NamedParameterJdbcTemplate 查询列表 /***测试***/ public void queyBeanTest(){ String s = "select * f ...
- CommandLineParser命令行解析类
目的:方便用户在命令行使用过程中减少工作量 以前版本没这个类时,如果要运行带参数的.exe,必须在命令行中输入文件路径以及各种参数,并且输入的参数格式要与代码中的if语句判断内容格式一样,一不小心就输 ...
- Redux 管理React Native数据
现在让我们看看大致的流程: React 可以触发 Action,比如按钮点击按钮. Action 是对象,包含一个类型以及相关的数据,通过 Store 的 dispatch() 函数发送到 Store ...
- Vue--vux组件库
各种组件demo源码~ https://doc.vux.li/zh-CN/
- _gift
time 本次上线后在线时长,单位:秒 rewId 奖励模板ID,对应_rew表中rewId notice 弹窗内容,为空不弹窗
- mariaDB中文乱码
cetos7 下 http://hongjun.blog.51cto.com/445761/400985 1 . copy 一个文件成 /etc/my.cnf cp /usr/share/mysql ...
- go helloworld
// Sample program to show how a bytes.Buffer can also be used // with the io.Copy function. package ...
- 【测试工程师面试】面试官热衷询问的N个问题
1. 数据库中左连接右连接的区别 2.JAVA中continue和break的区别 3.Linux中查看某一个进程并且杀死 1.数据库中多表连接,根据不同的表的某一个字段进行关联, 左连接是将左边表全 ...
- tips 移入悬浮功能
前景: 页面部分区域需要移入悬浮效果,当然默认的 title 也是可以的,最多只是格格不入,但是却是最为靠谱的.. 思路: 基于 jq 实例扩展 .使用立即执行函数保持功能独立. 自定义类实现功能封装 ...