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. 用keras作CNN卷积网络书本分类(书本、非书本)

    本文介绍如何使用keras作图片分类(2分类与多分类,其实就一个参数的区别...呵呵) 先来看看解决的问题:从一堆图片中分出是不是书本,也就是最终给图片标签上:“书本“.“非书本”,简单吧. 先来看看 ...

  2. C:数据结构与算法之单链表

    单链表相对于顺序表比较难理解,但是比较实用,单链表的插入,删除不需要移动数据元素,只需要一个指针来寻找所需要的元素,还有一个大优点就是不浪费空间,当你想要增加一个结点可以申请(malloc())一个结 ...

  3. vue使用国际化

    转载请注明作者与出处 一:安装vue-i18n npm install vue-i18n --save 二:定义不同语言的json语言包 一般把它放到npm工程中的src目录下,因为这个目录是要进行编 ...

  4. android操作系统在itop4412开发板上的烧写

    ITOP4412启动模式: 1.EMMC启动模式:拨码开关1-3为011 2.SD卡启动模式:拨码开关1-3为100uboot:初始化内存控制器,访问存储器,把操作系统内核从存储器读取出来放到内存中, ...

  5. windows net 命令(转载)

    CMD-NET命令详解 net命令大全,net命令用法,net网络命令,net命令使用,net命令集,net命令介绍,net常用命令,net命令的使用技巧,net命令如何使用 大家在操作Windows ...

  6. Using F2 to Rename Open Files

    Copy to your User keymap { "keys": ["shift+f2"], "command": "rena ...

  7. Linux(CentOS6.5)下编译安装Nginx官方最新稳定版(nginx-1.10.0)

    注:此文已经更新为新版:http://comexchan.cnblogs.com/p/5815753.html ,请直接查看新版,谢谢! 本文地址http://comexchan.cnblogs.co ...

  8. tomcat中使用mysql连接池的配置

    1.下载相应的jar包,添加到工程中 需要下载的包主要有commons-pool2-2.2 commons-dbcp2-2.0.1-src commons-dbcp2-2.0.1  commons-c ...

  9. 房上的猫:HTML5基础

    一.W3C标准 1)W3C标准不是某一个标准,而是一系列的标准的集合,一个网页主要由三部分组成,即结构(Structure),表现(Presentation)和行为(Behavior) 2)不很严谨的 ...

  10. BASIC-2 01字串

      基础练习 01字串   时间限制:1.0s   内存限制:256.0MB        问题描述 对于长度为5位的一个01串,每一位都可能是0或1,一共有32种可能.它们的前几个是: 00000 ...