题目链接

Problem 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 <iostream>
#include<string.h>
using namespace std;
int m,n;
int Next[4][2]= {{0,1},{1,0},{0,-1},{-1,0}}; int bj[21][21];
char a[21][21];
int sum=0;
void dfs(int x,int y)
{
int nx,ny;
for(int i=0; i<4; i++)
{
nx=x+Next[i][0];
ny=y+Next[i][1];
if(a[nx][ny]=='.'&&nx>=0&&nx<m&&ny>=0&&ny<n&&bj[nx][ny]==0)
{
bj[nx][ny]=1;
sum++;
dfs(nx,ny);
}
}
} int main()
{
int i,j,x,y;
while(cin>>n>>m)
{
if(m==0||n==0)
break;
for(i=0; i<m; i++)
for(j=0; j<n; j++)
{
cin>>a[i][j];
if(a[i][j]=='@')
{
x=i;
y=j;
}
}
memset(bj,0,sizeof(bj));
sum=1;
dfs(x,y);
cout<<sum<<endl;
}
return 0;
}

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

  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 (dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Oth ...

  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)

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

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

  8. HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)

    Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is ...

  9. HDU 2553 N皇后问题(深搜DFS)

    N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

随机推荐

  1. iframe 随内容自适应高度

    兼容性好的 html代码: <iframe src="enterprise/enter_edit.aspx" id="mainframe" framebo ...

  2. ubuntu下修改MySQL的配置文件my.cnf

    先sudo su转换成root,再用cd转到/etc/MySQL目录下,用chmod修改权限(chmod 755 my.cnf),但这样还不能修改,再用vi命令(vi my.cnf),通过上下方向键将 ...

  3. 【Docker 命令】- rmi命令

    docker rmi : 删除本地一个或多个镜像. 语法 docker rmi [OPTIONS] IMAGE [IMAGE...] OPTIONS说明: -f :强制删除: --no-prune : ...

  4. 统一日志系统 Log4Net/ExceptionLess

    一.   写在前面 本文Log4Net介绍了基础的方式,大数据量生产环境不能使用,中等日志量请日志单库. 希望爱技术的你不要错过exceptionless和ELK 第四节开始简单配置大牛们推荐的了Ex ...

  5. 求csdn博客优良编辑方法

    看见很多大牛的csdn博客编写的非常好,阅读体验也非常强.我就纳闷了,为啥我插公式也不行,插图片也不行呢... 插图片问题:图片不能复制招贴,否则在编辑的时候可以显示但是在发表之后就无法显示了.想要显 ...

  6. 第14天:逻辑运算符、if、for语句

    今天学习了逻辑运算符.if.for语句基础知识. 一.逻辑运算符 1.&&(与) 一假即假,同真为真2.||(或)一真即真,同假为假3.!(非)切记:参与逻辑运算的,都是布尔值.也就是 ...

  7. Go语言【第三篇】:Go变量和常量

    Go语言变量 变量来源于数学,是计算机语言中能存储计算结果或能表示值抽象概念.变量可以通过变量名访问.Go语言变量名由字母.数字.下划线组成,其中首字母不能为数字,声明变量的一般形式是使用var关键字 ...

  8. Luogu2540 斗地主增强版(搜索+动态规划)

    单纯的暴搜似乎还是很好写的,然而过不了.出完顺子之后答案是可以dp出来的,于是大力搜然后大力dp就好了. dp时强行讨论完了几乎所有拆牌情况,理性愉悦一发. #include<iostream& ...

  9. (三)Redis列表List操作

    List全部命令如下: lset key index value # 将列表key下标为index的元素的值设置为value,当 index 参数超出范围,或对一个空列表(key不存在)进行lset时 ...

  10. [洛谷P1552][APIO2012]派遣

    题目大意:有一棵$n$个点的树,和一个费用$m$,每个点有一个费用和价值,请选一个点,再从它的子树中选取若干个点,使得那个点的价值乘上选的点的个数最大,要求选的点费用总和小于等于$m$ 题解:树形$d ...