HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)
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
Asia 2004, Ehime (Japan), Japan Domestic
题目链接: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]={1,0,-1,0};
4 int _y[4]={0,-1,0,1};//相当于从第一象限开始顺时针转一周,对应的坐标轴上的点分别为(1,0),(0,-1),(-1,0),(0,1),就是数学意义上的方向向量
5 char str[30][30];
6 bool flag[30][30];//去判断点是否被标记过,DFS搜索一遍,被标记过记改点为1!
7 int n,m,ans;
8 void DFS(int x,int y)
9 {
10 for(int ii=0;ii<4;ii++)
11 {
12 int i=x+_x[ii];
13 int j=y+_y[ii];
14 if(i<n&&j<m&&i>=0&&j>=0&&flag[i][j]==0&&str[i][j]=='.')//边界条件
15 {
16 ans++;
17 flag[i][j]=1;
18 DFS(i,j);
19 }
20 }
21 }
22 int main()
23 {
24 while(scanf("%d%d",&m,&n)!=EOF)
25 {
26 if(m==0&&n==0)
27 break;
28 int x,y;
29 memset(flag,0,sizeof(flag));
30 for(int i=0;i<n;i++)
31 scanf("%s",str[i]);
32 for(int i=0;i<n;i++)
33 {
34 for(int j=0;j<m;j++)
35 {
36 if(str[i][j]=='@')//找到起始点
37 {
38 x=i;
39 y=j;
40 }
41 }
42 }
43 ans=0;
44 flag[x][y]=1;
45 DFS(x,y);
46 printf("%dn",ans+1);//第一个位置也算一个,所以+1
47 }
48 return 0;
49 }
- 文件操作常用函数
- Java基础-day09-对象;类;封装 学生管理系统
- two Pass方法连通域检测
- 【Java入门提高篇】Day14 Java中的泛型初探
- 使用shell脚本快速得到主备关系(r9笔记第93天)
- 【Java入门提高篇】Day13 Java中的反射机制
- 仿腾讯课堂固定滚动列表ReactNative组件
- Golang通过socket与java通讯
- Java基础-day09-基础题-对象;类;封装
- 通过shell脚本得到数据库的基本信息(一)(r9笔记第89天)
- iOS设备唯一标识的前世今生
- python 生成内嵌式字典(dict)-案例从python提取内嵌json写入mongodb
- Golang语言打印九九乘法表
- AVFoundation 框架初探究(四)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 用BurpSuit的Burpy插件搞定WEB端中的JS加密算法
- CentOS7下安装文档协作工具Confluence7.2.1
- caret包进行机器学习
- 容器化Go应用--基础镜像的未知时区问题
- 写给自己的Object和Function的3个灵魂拷问
- 48. Vue路由-使用命名视图实现经典布局
- 47. Vue使用children属性实现路由嵌套
- RecyclerView的缓存机制和内存优化
- 使用Anchore Enine来完善DevSecOps工具链
- PyTorch版CenterNet训练自己的数据集
- SpringBoot2 整合FreeMarker模板,完成页面静态化处理
- PHP多文件上传格式化
- NDK开发案例 | C/C++调用java层代码
- 深入理解 SecurityConfigurer 【源码篇】
- Swift 属性