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. .Net core使用Quartz.Net 实现定时任务

    很多情况下,我们需要完成一些定时执行的功能,用很多定时工具,像:hangfire,TimeJob,以及Quartz.net,不过quartz.net 比较精确一些,功能也比较强大,所以我选择了Quar ...

  2. window下pip install Scrapy报错解决方案

    1.首先打开https://www.lfd.uci.edu/~gohlke/pythonlibs/#twisted,找到对应版本的Twisted并下载到你的文件夹. 2.利用pip install命令 ...

  3. Nginx(haproxy)+keepalived+Tomcat双主高可用负载均衡

    周末的时候一个正在学Linux的朋友问我,高可用怎么玩?我和他微信了将近三个小时,把Nginx和haproxy双主高可用教给他了,今天突然想把这个给写进博客里,供给那些正在学习Linux系统的朋友们, ...

  4. Zookeeper -- 关于Zookeeper

    Zookeeper是什么? 分布式协调框架 Zookeeper中文件呈树形结构,树形结构下包含多个节点,称为Znode:zk中节点存储数据不超过1M,指得是Znode中存储数据不超过1M Zookee ...

  5. Go搭建一个博客系统

    go语言环境就不用多说了,版本肯定越高越好,这里用go1.10 先放着

  6. FPGA烧完程序之后,检测不到网口的

    原因:未给phy芯片添加复位 解决方法:在程序顶部添加一个输出信号output e_reset,使其值一直为高. output e_reset, 'b1;

  7. [WebService] 使用httpWebrequest 调用并调试WebService

    使用httpWebrequest 调用并调试WebService. 首先  使用httpWebrequest 调用WebService 代码: using System.Net;            ...

  8. LeetCode: 63. Unique Paths II(Medium)

    1. 原题链接 https://leetcode.com/problems/unique-paths-ii/description/

  9. ORB-SLAM(十一)EPnP

    EPnP在ORB-SLAM中主要用于Tracking线程中的重定位Relocalization模块,需要通过当前关键帧Bow与候选帧匹配上的3D地图点,迅速建立当前相机的初始姿态. PnP问题解决了已 ...

  10. beego 点滴

    在使用beego时遇到 need a primary key field 1 确保结构中的 字段首字母大写 2 beego默认主键是id 如果主键定义的是其他字段比如userid 那么加上orm pk ...