题目链接

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. iOS-开发将文本复制到剪切板

    下面方法可以将文本复制到剪切板 UIPasteboard *pboard = [UIPasteboard generalPasteboard]; pboard.string = @"邀请码& ...

  2. 【Linux】- CentOS 7 安装.NET Core 2.1

    添加dotnet产品Feed 在安装.NET Core之前,您需要注册Microsoft产品Feed. 这只需要做一次. 首先,注册Microsoft签名密钥,然后添加Microsoft产品Feed. ...

  3. Perfmon - Windows 自带系统监控工具

    一. 简述 可以用于监视CPU使用率.内存使用率.硬盘读写速度.网络速度等. Perfmon提供了图表化的系统性能实时监视器.性能日志和警报管理,系统的性能日志可定义为二进制文件.文本文件.SQLSE ...

  4. parse_str — 将字符串解析成多个变量

    $arr2="first=value1&second=value2&third[]=value3&third[]=value4"; parse_str($a ...

  5. jquery计算器(改良版)

    代码: <!Doctype html> <html> <meta charset="UTF-8"> <title>计算器</t ...

  6. File文件以及.propertites文件操作

    File文件操作 在jsp和class文件中调用的相对路径不同.在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用Syste ...

  7. Delphi中Sender对象的知识

    Sender是一个TObject类型的参数,它告诉Delphi哪个控件接收这个事件并调用相应的处理过程.你可以编写一个单一的事件处理句柄,通过Sender参数和IF…THEN…语句或者CASE语句配合 ...

  8. bzoj4501 旅行

    题面: 小C来到了F国,小C想好好地参观F国.F国可以看一个有n个点m条边的有向无环图,小C刚开始站在1号点.假设现在小C站在x号点: 1.点x没有出边,结束旅游. 2.点x有o条出边,小C等概率地选 ...

  9. BZOJ4589:Hard Nim——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=4589 Claris和NanoApe在玩石子游戏,他们有n堆石子,规则如下: 1. Claris和N ...

  10. React组件通信

    1.父子通信 父 -> 子 props子 -> 父 回调函数,父组件通过props向子组件传递一个函数,子组件调用函数,父组件在回调函数中用setState改变自身状态 2.跨层级通信 1 ...