Red and Black

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 19731    Accepted Submission(s): 11991

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
 
Source
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312
分析:看到ZERO已经写了DFS,弱鸡也得补上此题,来一发!感谢梅大大的支持,代码的模版来自梅大大,我只是对主要步骤做了注释而已!
下面给出AC代码:

 #include <bits/stdc++.h>
using namespace std;
int _x[]={,,-,};
int _y[]={,-,,};//相当于从第一象限开始顺时针转一周,对应的坐标轴上的点分别为(1,0),(0,-1),(-1,0),(0,1),就是数学意义上的方向向量
char str[][];
bool flag[][];//去判断点是否被标记过,DFS搜索一遍,被标记过记改点为1!
int n,m,ans;
void DFS(int x,int y)
{
for(int ii=;ii<;ii++)
{
int i=x+_x[ii];
int j=y+_y[ii];
if(i<n&&j<m&&i>=&&j>=&&flag[i][j]==&&str[i][j]=='.')//边界条件
{
ans++;
flag[i][j]=;
DFS(i,j);
}
}
}
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
if(m==&&n==)
break;
int x,y;
memset(flag,,sizeof(flag));
for(int i=;i<n;i++)
scanf("%s",str[i]);
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
if(str[i][j]=='@')//找到起始点
{
x=i;
y=j;
}
}
}
ans=;
flag[x][y]=;
DFS(x,y);
printf("%d\n",ans+);//第一个位置也算一个,所以+1
}
return ;
}

HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)的更多相关文章

  1. HDU 5122 K.Bro Sorting(模拟——思维题详解)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an A ...

  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 --- 入门搜索 BFS解法

    HDU 1312 题目大意: 一个地图里面有三种元素,分别为"@",".","#",其中@为人的起始位置,"#"可以想象 ...

  5. POJ 1321 棋盘问题(DFS板子题,简单搜索练习)

    棋盘问题 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44012   Accepted: 21375 Descriptio ...

  6. SQLServer 常见SQL笔试题之语句操作题详解

    SqlServer 常见SQL笔试题之语句操作题详解 by:授客 QQ:1033553122 测试数据库 CREATE DATABASE handWriting ON PRIMARY ( name = ...

  7. 牛客网 Java 工程师能力评估 20 题 - 详解

    牛客网 Java 工程师能力评估 20 题 - 详解 不知在看博客的你是否知道 牛客网,不知道就太落后了,分享给你 : 牛客网 此 20 题,绝对不只是 20 题! 免责声明:本博客为学习笔记,如有侵 ...

  8. 关于SQL的几道小题详解

    关于SQL的几道小题详解 当我们拿到题目的时候,并不是急于作答,那样会得不偿失的,而是分析思路,采用什么方法,达到什么目的,还要思考有没有简单的方法或者通用的方法等等,这样才会达到以一当十的效果,这样 ...

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

随机推荐

  1. 小白的Python之路 day4 迭代器

    迭代器 学习前,我们回想一下可以直接作用于for循环的数据类型有以下几种: 1.集合数据类型,如list.tuple.dict.set.str等: 2.是generator,包括生成器和带yield的 ...

  2. 高性能管线式HTTP请求(实践·原理·实现)

      该篇实际是介绍pipe管线的原理,下面主要通过其高性能的测试实践,解析背后数据流量及原理.最后附带一个简单的实现     实践 先直接看对比测试方法 对于单一客户端对服务器进行http请求,一般我 ...

  3. URL加载页面的过程

    总体过程: 1.DNS解析 2.TCP连接 3.发送HTTP请求 4.服务器处理请求并返回HTTP报文 5.浏览器解析渲染页面 6.连接结束 一.DNS解析 在互联网中,每一台机计算机的唯一 标识是他 ...

  4. find 命令的误差估值与单位调整

    一.命令简介 find 命令的 -size 参数 单位b(不是byte而是block).c.w.k.M.G.默认是单位b ,也就是1block = 512byte = 0.5kb (文件系统ext4) ...

  5. linux部署solr服务--小记

    1.将solr压缩包上传到web项目-solr文件夹下 2.解压solr-5.5.4.zip到当前文件夹下 linux 解压zip文件到当前目录 unzip filename.zip 提示没有unzi ...

  6. eclipse 更换 JDK 版本后报错

    在实际开发过程中,可能由于项目的需要,我们需要更换 JDK 的版本.但是更换后会报错,如下: Java compiler level does not match the version of the ...

  7. MFC鼠标单击截获鼠标双击事件,且无法记录单击的数据的解决方案

    遇到的问题: 鼠标点击会截断鼠标双击的事件,无法保存椭圆的数据.也就是说双击执行的过程是OnLButtonDown,OnLButtonUp,OnLButtonDblClk,OnLButtonUp.并不 ...

  8. 用C#实现微信“跳一跳”小游戏的自动跳跃助手

    一.前言: 前段时间微信更新了新版本后,带来的一款H5小游戏“跳一跳”在各朋友圈里又火了起来,类似以前的“打飞机”游戏,这游戏玩法简单,但加上了积分排名功能后,却成了“装逼”的地方,于是很多人花钱花时 ...

  9. class, classloder, dex 详解

    class与dex文件 什么是class文件 class文件是一种能够被JVM识别,加载并且执行的文件格式. class文件的作用 class文件的作用是记录一个类文件的所有信息. 例如记住了当前类的 ...

  10. SAP中的BOPF(Business Object Processing Framework)

    希望简化你的业务应用开发过程?业务对象处理框架(Business Object Processing Framework,以下简称BOPF)也许可以帮到你. BOPF是SAP Business Sui ...