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<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
char map[][];
int vis[][];
int dir[][]={{,},{,-},{,},{-,}};
int n,m,num;
void DFS(int x,int y)
{
int a,b,i;
vis[x][y]=;
num++;
for(i=;i<;i++)
{
a=x+dir[i][];
b=y+dir[i][];
if(a>=&&a<m&&b>=&&b<n&&vis[a][b]==&&map[a][b]=='.')
{
DFS(a,b);
}
}
}
int main()
{
int i,j,x,y;
while(scanf("%d%d",&n,&m)!=EOF)
{
getchar();
if(m==&&n==)
break;
memset(map,,sizeof(map));
memset(vis,,sizeof(vis));
for(i=; i<m; i++)
{
scanf("%s",map[i]);
}
for(i=; i<m; i++)
{
for(j=; j<n; j++)
{
if(map[i][j]=='@')///只有一个人
{
x=i;
y=j;
}
}
}
num=;
DFS(x,y);
printf("%d\n",num);
}
return ;
}
 
 
 
 

Red and Black(DFS深搜实现)的更多相关文章

  1. CodeM美团点评编程大赛初赛B轮 黑白树【DFS深搜+暴力】

    [编程题] 黑白树 时间限制:1秒 空间限制:32768K 一棵n个点的有根树,1号点为根,相邻的两个节点之间的距离为1.树上每个节点i对应一个值k[i].每个点都有一个颜色,初始的时候所有点都是白色 ...

  2. DFS 深搜专题 入门典例 -- 凌宸1642

    DFS 深搜专题 入门典例 -- 凌宸1642 深度优先搜索 是一种 枚举所有完整路径以遍历所有情况的搜索方法 ,使用 递归 可以很好的实现 深度优先搜索. 1 最大价值 题目描述 ​ 有 n 件物品 ...

  3. DFS深搜——Red and Black——A Knight&#39;s Journey

    深搜,从一点向各处搜找到全部能走的地方. Problem Description There is a rectangular room, covered with square tiles. Eac ...

  4. 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目

    [题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  5. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  6. UVA 165 Stamps (DFS深搜回溯)

     Stamps  The government of Nova Mareterrania requires that various legal documents have stamps attac ...

  7. HDU 1312 Red and Black (深搜)

    题目链接 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore ...

  8. CCF 模拟E DFS深搜

    http://115.28.138.223:81/view.page?opid=5 这道题问的很怪. 起点DFS,每一个点还要DFS一次,统计不能到终点的个数 数据量不大这样做也能AC #includ ...

  9. HDU_1241 Oil Deposits(DFS深搜)

    Problem Description The GeoSurvComp geologic survey company is responsible for detecting underground ...

随机推荐

  1. ABAP术语-SAP GUI for HTML

    SAP GUI for HTML 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/14/1104996.html An ITS impleme ...

  2. Linux——查看

    查看当前系统版本: lsb_release -a #没有装:yum install lsb 查看当前运行端口: netstat -atunlp #没有装:yum install net-tools - ...

  3. centos6,python3,通过pip安装pycurl出现报错提示

    Centos6.7系统,python3.6.7,通过 pip 安装pycurl出现报错: __main__.ConfigurationError: Could not run curl-config: ...

  4. awk命令用法

    awk:把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各种分析处理,是一个强大的文本分析工具,在对数据分析并生成报告时很有优势. awk有3个不同版本: awk.nawk和gawk, ...

  5. vue调用豆瓣API加载图片403问题

    "豆瓣API是有请求次数限制的”,这会引发图片在加载的时候出现403问题,视图表现为“图片加载不出来”,控制台表现为报错403. 其实是豆瓣限制了图片的加载,我自己用了一个办法把图片缓存下来 ...

  6. js 中~~是什么意思?

    其实是一种利用符号进行的类型转换,转换成数字类型 ~~true == 1~~false == 0~~"" == 0~~[] == 0 ~~undefined ==0~~!undef ...

  7. FMX有一套自己的消息处理机制。类似这样:

    unit Unit1; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Var ...

  8. python3 安装pyhanlp方法

    直接pip install pyhanlp的时候会提示缺少Microsoft Visual c++环境, 其实没有Microsoft Visual c++环境也是可以的, 可以先安装jpype1,然后 ...

  9. cocos2d中锚点概念

    这两天看了下锚点的概念. /** * Sets the anchor point in percent. * * anchorPoint is the point around which all t ...

  10. UPDATE_ENTITY実行

    1.クラスZCL_Z_EPM_RKT_DPC_EXTのクラスビルダ画面から.SALESORDERS_UPDATE_ENTITYメソッドを選択し.右クリックで.再定義をクリックします. 2.以下のソース ...