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 题目大意:有一间矩形房屋,地上铺了红.黑两种颜色的方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相 ...
随机推荐
- 如何使用jqueryUi的datepicker日历控件?
参考: http://www.jb51.net/article/85007.htm 这里的日历控件是, 基于jquery的jqureyui中的一个 widget. 需要js 文件: 外部的js文件, ...
- 【索引失效】什么情况下会引起MySQL索引失效
索引并不是时时都会生效的,比如以下几种情况,将导致索引失效: 1.如果条件中有or,即使其中有条件带索引也不会使用(这也是为什么尽量少用or的原因) 注意:要想使用or,又想让索引生效,只能将or条件 ...
- 常用for循环和for in 以及for of 的区别
用Es6对象扩展运算符(…)与rest运算符说明 function test(first,...a){ for(let val=0; val<a.length;val++){ console.l ...
- 17秋 SDN课程 第五次上机作业
17秋 SDN课程 第五次上机作业 Project:https://github.com/Wasdns/new_balance Slide is available at https://github ...
- js之鼠标随动后面跟随事件(类似于长龙跟着跑)
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- CPU核数和load average的关系
在前面的文章<Linux系统监控——top命令>中我简单提到了,判断load average的数值到底大不大的判断依据,就是数值除以CPU核数,大于5,就说明超负荷运转了.——这里其实不太 ...
- ERROR: child process failed, exited with error number 100
[root@localhost ~]# mongod --dbpath=/usr/local/mongodb/data --logpath=/usr/local/mongodb/logs --loga ...
- 快排+java实现
import java.util.Arrays; public class QuickSort { //三数取中法.取出不大不小的那个位置 public static int getPivotPos( ...
- 记录flask使用模板时出现的“Internal Server Error”错误
在看<Flask Web开发实战:入门.进阶与原理解析(李辉著 )>时照着书上的代码抄了一遍,然后运行时发现一直出现以下的错误 书上的源代码如下 watchlist.html <he ...
- CC2 条理分明-----独立思考
独立思考 前几天啊,在吃饭的时候,听到同事们在讨论某幼儿园事件,因为没有人愿意出来作证,所以很可能是造谣.前几天他们还咬牙切齿的指责这个幼儿园,现在怎么就变了.我发现人们的思维变化的太快,我自己也是的 ...