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. 关于MAX()函数的一点思考

    本文同时发表在https://github.com/zhangyachen/zhangyachen.github.io/issues/103 考虑如下表和sql: CREATE TABLE `ikno ...

  2. ES6 let和const命令(3)

    const 用来声明常量.一旦声明,就不能改变. const在声明必须初始化,只声明不赋值会出错 const的作用域与let一样,只在声明的块级作用域有效. const命令声明的常量也不提升,同样存在 ...

  3. 安装supervisord

    一:简介 supervisord是一个进程管理工具,提供web页面管理,能对进程进行自动重启等操作. 优点: - 可以将非后台运行程序后台运行 - 自动监控,重启进程 缺点: - 不能管理后台运行程序 ...

  4. C# 各种帮助类大全

    前言 此篇专门记录一些常见DB帮助类及其他帮助类,以便使用时不用重复造轮子. DBHelper帮助类 ①首当其冲的就是Sql Server帮助类,创建名为DbHelperSQL 的类 ,全部代码如下: ...

  5. windows 安装Mysql压缩包

    1.将mysql压缩包解压至选定目录下 2.修改my-default.ini文件的以下内容 basedir = E:\develope_software\MySQL\mysql-advanced-5. ...

  6. Kettle中忽略错误行继续执行

    在kettle执行的过程中,如果遇到错误,kettle会停止运行.在某些时候,并不希望kettle停止运行,所以就要处理下这些错误行. 例如这两天发现在一个转换中,总数出现一些不规则数据,这些数据一出 ...

  7. 关于vs2010下水晶报表的使用入门

    关于vs2010下使用水晶报表了解情况记录如下: 1.首先vs2010不再自带水晶报表控件了,需要下载安装vs2010配套的水晶报表控件:CRforVS_13_0.这个控件安装很简单,基本上都选择默认 ...

  8. Head First设计模式之备忘录模式

    一.定义 不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样就可以将该对象恢复到原先保存的状态 二.结构 备忘录模式中主要有三类角色: 发起人角色:记录当前时刻的内部状态, ...

  9. python简单爬虫技术

    项目中遇到这个只是点,捣鼓了半天最后没用上,但是大概对爬虫技术有了些许了解 要先 比如: #抓取网页代码 import urllib2 import json url_data = urllib2.u ...

  10. python 发信实例

    转自:http://www.cnblogs.com/lonelycatcher/archive/2012/02/09/2343463.html 文件形式邮件   #!/usr/bin/env pyth ...