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代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int _x[]={,,-,};
  4. int _y[]={,-,,};//相当于从第一象限开始顺时针转一周,对应的坐标轴上的点分别为(1,0),(0,-1),(-1,0),(0,1),就是数学意义上的方向向量
  5. char str[][];
  6. bool flag[][];//去判断点是否被标记过,DFS搜索一遍,被标记过记改点为1!
  7. int n,m,ans;
  8. void DFS(int x,int y)
  9. {
  10. for(int ii=;ii<;ii++)
  11. {
  12. int i=x+_x[ii];
  13. int j=y+_y[ii];
  14. if(i<n&&j<m&&i>=&&j>=&&flag[i][j]==&&str[i][j]=='.')//边界条件
  15. {
  16. ans++;
  17. flag[i][j]=;
  18. DFS(i,j);
  19. }
  20. }
  21. }
  22. int main()
  23. {
  24. while(scanf("%d%d",&m,&n)!=EOF)
  25. {
  26. if(m==&&n==)
  27. break;
  28. int x,y;
  29. memset(flag,,sizeof(flag));
  30. for(int i=;i<n;i++)
  31. scanf("%s",str[i]);
  32. for(int i=;i<n;i++)
  33. {
  34. for(int j=;j<m;j++)
  35. {
  36. if(str[i][j]=='@')//找到起始点
  37. {
  38. x=i;
  39. y=j;
  40. }
  41. }
  42. }
  43. ans=;
  44. flag[x][y]=;
  45. DFS(x,y);
  46. printf("%d\n",ans+);//第一个位置也算一个,所以+1
  47. }
  48. return ;
  49. }

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. HTML5 进阶系列:拖放 API 实现拖放排序(转载)

    HTML5之拖放API实现拖放排序 前言 HTML5 中提供了直接拖放的 API,极大的方便我们实现拖放效果,不需要去写一大堆的 js,只需要通过监听元素的拖放事件就能实现各种拖放功能. 想要拖放某个 ...

  2. Python学习日记day10------函数的命名空间、作用域与闭合函数

    1,参数陷阱 如果默认参数的只是一个可变数据类型,那么每一次调用的时候,如果不传值就共用这个数据类型的资源. 2,三元运算 c=a if a>b else b#如果a>b返回a,否则,返回 ...

  3. Python学习日记day3:数据类型

    1.数据类型int :如1.2.4等, 用于计算 bool: True , False ,用户判断 str: 储存少量数据,进行操作.如:'fdasklfjfladfl','而而噩噩','1234' ...

  4. xml生成方式二(Xml序列化器XmlSerializer)

    一.andoirdAPI提供了xml生成和解析的API: XmlSerializer xs = Xml.newSerializer();和XmlPullParser xmlPullParser = X ...

  5. 关于MAX()函数的一点思考

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

  6. sql server 2012 新知识-序列

    今天聊一聊sql 2012 上的新功能-----序列 按我的理解,它就是为了实现全局性的唯一标识,按sql server 以前的版本,想对一张表标识很简单,比如identity,但如果要对某几张有业务 ...

  7. asp.net core 2.0+sqlsugar搭建个人网站系列(0)

    一些废话 马上就要过年了,回顾这一年最大的收获就是技术有了很大的提升,其他的方面没有什么改变,现在还是单身小屌丝一枚. 这一年来学习的主要重点就是asp.net core,中间也使用 core+EF做 ...

  8. ArrayList中对象 排序

    public class Student implements Comparable { private String studentname; public int studentage; publ ...

  9. IT服务(运维)管理实施的几个要点--第二章 人员和组织架构

    子曰"没有合适的人" 在流程化的管理模式下,最容易步入的一个误区是按流程设计一个"理想的"组织架构,然后对应于这个架构对人员进行评估.培养,甚至是更换.我见过很 ...

  10. [编织消息框架][JAVA核心技术]动态代理应用12-总结

    动态代理这篇比较长,是框架组成的重要基础 回顾下学到的应用技术 1.异常应用 2.annotation技术 3.数值与逻辑分享 4.jdk.cglib.javassist等动态代理技术 5.懒处理.预 ...