数据结构——HDU1312:Red and Black(DFS)
题目描述
Write a program to count the number of black tiles which he can reach by repeating the moves described above.
输入
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)
The end of the input is indicated by a line consisting of two zeros.
Output
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 解题思路:
深搜的方法解决,题目意思就是从@开始找.并与@连通,碰到#等于碰到了墙,题目很简单,@可以向四个方向上、下、左、右走,所以 用四个坐标标记出来,然后,再一一遍历,递归调用寻找,用一个30*30的数组标识此点有没有走过,避免走重复 程序代码:
#include <cstdio>
#include <cstring>
using namespace std;
int n,m,cot;
char map[][];
int to[][] = {{,},{,},{-,},{,-}}; void dfs(int i,int j)
{
cot++;
map[i][j] = '#';
for(int k = ; k<; k++)
{
int x = i+to[k][];
int y = j+to[k][];
if(x<n && y<m && x>= && y>= && map[x][y] == '.')
dfs(x,y);
}
return;
} int main()
{
int i,j,fi,fj;
while(~scanf("%d%d%*c",&m,&n)&&m&&n)
{
for(i = ; i<n; i++)
{
for(j = ; j<m; j++)
{
scanf("%c",&map[i][j]);
if(map[i][j] == '@')
{
fi = i;
fj = j;
}
}
getchar();
}
cot= ;
dfs(fi,fj);
printf("%d\n",cot);
} return ;
}
数据结构——HDU1312:Red and Black(DFS)的更多相关文章
- HDU1312——Red and Black(DFS)
Red and Black Problem DescriptionThere is a rectangular room, covered with square tiles. Each tile i ...
- HDU1312 Red and Black(DFS) 2016-07-24 13:49 64人阅读 评论(0) 收藏
Red and Black Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- HDU1312 Red and Black(dfs+连通性问题)
这有一间铺满方形瓷砖的长方形客房. 每块瓷砖的颜色是红色或者黑色. 一个人站在一块黑色瓷砖上, 他可以从这块瓷砖移动到相邻(即,上下左右)的四块瓷砖中的一块. 但是他只能移动到黑色瓷砖上,而不能移动到 ...
- HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...
- HDU 1312 Red and Black (DFS)
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...
- HDU 1312 Red and Black(DFS,板子题,详解,零基础教你代码实现DFS)
Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- 数据结构:关键路径,利用DFS遍历每一条关键路径JAVA语言实现
这是我们学校做的数据结构课设,要求分别输出关键路径,我查遍资料java版的只能找到关键路径,但是无法分别输出关键路径 c++有可以分别输出的,所以在明白思想后自己写了一个java版的 函数带有输入函数 ...
- HDOJ1312 Red and black(DFS深度优先搜索)
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...
- hdu1312 Red and Black
I - Red and Black Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
随机推荐
- jetty服务器访问系统的域名
jetty-env.xml=><Set name="contextPath">/epps-compensation-backend</Set> 这个决 ...
- TypeScript笔记[5]泛型+Dictionary 转
TypeScript笔记[5]泛型 在C++.C#.Java等主流编程语言中,一般对泛型编程提供了支持.合理利用泛型,可以提高开发效率.提升代码质量. 例如在C++编程语言中,常常利用下面的结构表 ...
- java 反射调用支付SDK
在android开发中会遇到各种SDK的接入,很是麻烦.最初在想能不能把所有的SDK都 融合到一个当中,发现有点异想天开.但是也可以解决SDK资源不小心没有引入,导致程序调用接口崩溃问题.经过查资料, ...
- oracle数据表误删恢复
1.查看回收站中的表: select object_name,original_name,partition_name,type,ts_name,createtime,droptime from re ...
- ASP.NET实现二级域名(多用户,多商店)
本人所了解有两种方案,可能还有其的方式,希望大家多多讨论! 基本思路: 1. 域名支持泛解析,即是指:把A记录 *.域名.com 解析到服务器IP,服务器IIS中做绑定,绑定时主机头为空; 2. ...
- java问题整理
1.一个“.java”源文件中是否可以包括多个类(不是内部类)?有什么限制? 答:可以有多个类.但只能有一个public类.并且public类名必须与文件名相一致. 2.Java有没有goto? ...
- 线程取消 (pthread_cancel)
线程取消(pthread_cancel) 基本概念pthread_cancel调用并不等待线程终止,它只提出请求.线程在取消请求(pthread_cancel)发出后会继续运行,直到到达某个取消点(C ...
- 解决linux .so的链接时符号依赖问题
问题描述 target: a.out SO:libmyfile.so 依赖描述: a.out: libmyfile.so libmyfile.so: libssl.so.1.0.0 libssl.s ...
- C# 控制台程序设置字体颜色
这几天做了个程序,程序本身很简单.大体功能是输入查询条件,从数据库里取出结果计算并显示.但是用户的要求是使用控制台(console)来实现功能.由于功能简单,程序很快就做完了,在面向用户演示程序时,突 ...
- 移动端版本兼容js
移动端版本兼容js <!--移动端版本兼容 --> <script type="text/javascript"> var phoneWidth = par ...