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>
using namespace std;
char s[][];
int n,m;
int dfs(int x,int y)
{
int i,j,k;
if(x<||x>n-||y<||y>m-)
return ; if(s[x][y]=='#')
{
return ;
}
else
{
s[x][y]='#';
return +dfs(x+,y)+dfs(x-,y)+dfs(x,y+)+dfs(x,y-);//搜索四个方向
}
}
int main()
{
int i,j,k;
while(scanf("%d%d",&m,&n)!=EOF)
{
if(m==&&n==)
break;
for(i=;i<n;i++)
{
scanf("%s",s[i]);
}
int sum=;
for(i=;i<n;i++)
{
for(j=;j<m;j++)
{
if(s[i][j]=='@')
{
sum=dfs(i,j);
}
}
}
printf("%d\n",sum);
}
return ;
}

永不言弃!和ACM死磕到底!

HDOJ1312 Red and black(DFS深度优先搜索)的更多相关文章

  1. HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  2. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  3. HDU 4707 Pet(DFS(深度优先搜索)+BFS(广度优先搜索))

    Pet Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submissio ...

  4. 回溯算法 DFS深度优先搜索 (递归与非递归实现)

    回溯法是一种选优搜索法(试探法),被称为通用的解题方法,这种方法适用于解一些组合数相当大的问题.通过剪枝(约束+限界)可以大幅减少解决问题的计算量(搜索量). 基本思想 将n元问题P的状态空间E表示成 ...

  5. 『ACM C++』HDU杭电OJ | 1416 - Gizilch (DFS - 深度优先搜索入门)

    从周三课开始总算轻松了点,下午能在宿舍研究点题目啥的打一打,还好,刚开学的课程还算跟得上,刚开学的这些课程也是复习以前学过的知识,下半学期也不敢太划水了,被各种人寄予厚望之后瑟瑟发抖,只能努力前行了~ ...

  6. 步步为营(十五)搜索(一)DFS 深度优先搜索

    前方大坑预警! 先讲讲什么是搜索吧. 有一天你去一个果园摘梨子,果农告诉你.有一棵树上有一个金子做的梨子,找到就是你的,你该怎么找? 地图例如以下: S 0 0 0 0 0 0 0 0 0 0 0 0 ...

  7. [算法总结]DFS(深度优先搜索)

    目录 一.关于DFS 1. 什么是DFS 2. DFS的搜索方式 二.DFS的具体实现 三.剪枝 1. 顺序性剪枝 2. 重复性剪枝 3. 可行性剪枝 4. 最优性剪枝 5. 记忆化剪枝 四.练习 一 ...

  8. 回溯 DFS 深度优先搜索[待更新]

      首先申明,本文根据微博博友 @JC向北 微博日志 整理得到,本文在这转载已经受作者授权!   1.概念   回溯算法 就是 如果这个节点不满足条件 (比如说已经被访问过了),就回到上一个节点尝试别 ...

  9. DFS——深度优先搜索的一般格式

    DFS是一种深度优先的搜索思想,运用递归完成搜索,本质上也算是穷举思想的一类,可以通过剪枝进行优化. DFS的核心是回溯和递归, 如果以迷宫为例,一般会指定走各个方向的顺序(例如先左再上再右再下).从 ...

随机推荐

  1. git命令行在windows中报错WARNING: terminal is not fully functional

    今天在windows的PowerShell中执行git log命令的时候报错: WARNING: terminal is not fully functional 网上查找方法,最后 1.打开wind ...

  2. RobotFramework环境配置:默认以管理员权限运行cmd

    设置cmd以管理员权限运行 目的:创建或删除文件等命令时,需要管理员权限运行cmd(linux以root用户登录).   例如,创建日志目录.   方法一: 1.激活administrator用户 2 ...

  3. iOS开发支付篇-内购(IAP)

    一,前言 经典文章参考: . http://yimouleng.com/2015/12/17/ios-AppStore/ 内购流程 . http://www.jianshu.com/p/b199a46 ...

  4. Django ORM中datetiem数据类型字段无法对比处理办法

    在做商城项目中浏览足迹时,我利用浏览商品的ID和浏览的时间保存到browse表中,然后在我的足迹页面根据最近日期进行展示 条件:每天/个商品只能如一次表 后台代码如下: #存储浏览足迹到browse表 ...

  5. IIS宿主WCF服务*.svc Mime类型映射

    经常会遇到由于.net安装组件缺失,导致发布wcf服务后,访问wcf报.svc请求类型不支持 简单方法就是添加删除程序,修改.net组件安装选项,勾选http激活即可: 或者手工添加映射处理程序 1. ...

  6. 2.Hadoop平台架构准备工作

    1. 需要的软件:centos.hadoop.jdk.winscp. 2.搭建开发环境 Vmware安装 3.安装Linux操作系统 (1).安装虚拟机1,设置相关的参数: 4.点击设置,常规-> ...

  7. 2019/4/22 拓扑排序的高效写法. 模板题HDU1285:确定比赛名次

    传送门 Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现 ...

  8. 007-chrome插件系列

    1.Axure RP Extension for Chrome 2.Charset 3.CLEAN crxMouse Gestures 4.Google 翻译 5.JSONView 6.restlet

  9. Python加密保护-对可执行的exe进行保护

    Python 是一种面向对象的解释型计算机程序设计语言,Python 语言写的程序不需要编译成二进制代码,可以直接从源代码运行程序. 在计算机内部,Python解释器把源代码转换成称为字节的中间形式, ...

  10. [ROS]激光驱动安装

    参考资料: https://blog.csdn.net/hongliang2009/article/details/73302986 https://blog.csdn.net/bohaijun_12 ...