DFS(4)——hdu1010Tempter of the Bone
一、题目回顾
题目链接:Tempter of the Bone
The maze was a rectangle with sizes N by M. There was a door in the
maze. At the beginning, the door was closed and it would open at the
T-th second for a short period of time (less than 1 second). Therefore
the doggie had to arrive at the door on exactly the
T-th second. In every second, he could move one block to one of the
upper, lower, left and right neighboring blocks. Once he entered a
block, the ground of this block would start to sink and disappear in the
next second. He could not stay at one block for
more than one second, nor could he move into a visited block. Can the
poor doggie survive? Please help him.
The first line of each test case contains three integers N, M, and T (1
< N, M < 7; 0 < T < 50), which denote the sizes of the maze
and the time at which the door will open, respectively.
The next N lines give the maze layout, with each line containing M
characters. A character is one of the following:
'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door;
'.': an empty block.
The input is terminated with three 0's. This test case is not to be processed.
- DFS
- 剪枝
注意,这道题目是要恰好t时间到达,并不是在t时间内到达......

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std; int n,m,t;
int sx,sy,dx,dy;
int flag;
char a[][];
bool vis[][]; //标记某个点是否已经过
int to[][] = {{,},{-,},{,},{,-}}; void dfs(int x,int y,int k) //小狗当前的坐标(x,y) 及 第 k 秒
{
if(k>t) return; //超过规定时间
if(k==t && !(x==dx&&y==dy)) return; //任务失败
if(k==t && x==dx && y==dy){ //任务成功
flag = ; //此标记为了让调用回到主函数
return;
}
for(int i=;i<;i++){ //小狗在点(x,y)向四个方向走
int mx = x+to[i][];
int my = y+to[i][];
if(a[mx][my]!='X' && !vis[mx][my]){
if(mx< || mx>n || my< || my>m) continue; //越界
vis[mx][my] = ;
dfs(mx,my,k+);
if(flag) return;
vis[mx][my] = ;
}
}
} int main()
{
while(scanf("%d%d%d",&n,&m,&t) && !(n==&&m==&&t==)){
for(int i=;i<=n;i++){
getchar();
for(int j=;j<=m;j++){
scanf("%c",&a[i][j]);
if(a[i][j] == 'S'){
sx = i;
sy = j;
}
if(a[i][j] == 'D'){
dx = i;
dy = j;
}
}
}
getchar(); if(t<abs(dx+dy-sx-sy)){ //剪枝一
printf("NO\n"); continue;
}
int a = (sx+sy)%, b = (dx+dy)%; //剪枝二
int c = (a+b)%, d = t%;
if(c != d){
printf("NO\n"); continue;
} flag = ;
memset(vis,,sizeof(vis));
vis[sx][sy] = ;
dfs(sx,sy,);
if(flag == ) printf("YES\n");
else printf("NO\n");
}
return ;
}
DFS(4)——hdu1010Tempter of the Bone的更多相关文章
- DFS(深度优先)算法编程实践
DFS定义 DFS(Depth-First-Search)深度优先搜索算法,是搜索算法的一种.是一种在开发爬虫早期使用较多的方法.它的目的是要达到被搜索结构的叶结点 . 特点 每次深度优先搜索的结果必 ...
- 拓扑排序+DFS(POJ1270)
[日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...
- DFS(一):深度优先搜索的基本思想
采用搜索算法解决问题时,需要构造一个表明状态特征和不同状态之间关系的数据结构,这种数据结构称为结点.不同的问题需要用不同的数据结构描述. 根据搜索问题所给定的条件,从一个结点出发,可以生成一个或多个新 ...
- 深度优先搜索DFS(一)
实例一 0/1背包问题: 有n件物品,每件物品的重量为w[i],价值为c[i].现在需要选出若干件物品放入一个容量为V的背包中,使得在选入背包的物品重量和不超过容量V的前提下,让背包中的物品 ...
- 万能的搜索--之DFS(二)
(一)深度优先搜索(DFS) 我们先给出深度优先的解决办法,所谓深度优先搜索,在迷宫问题里就是不撞南墙不回头,能走得深一点就尽量深一点.如果碰到了墙壁就返回前一个位置尝试其他的方向.在<啊哈!算 ...
- DFS(二):骑士游历问题
在国际象棋的棋盘(8行×8列)上放置一个马,按照“马走日字”的规则,马要遍历棋盘,即到达棋盘上的每一格,并且每格只到达一次.例如,下图给出了骑士从坐标(1,5)出发,游历棋盘的一种可能情况. [例1] ...
- DFS(四):剪枝策略
顾名思义,剪枝就是通过一些判断,剪掉搜索树上不必要的子树.在采用DFS算法搜索时,有时候我们会发现某个结点对应的子树的状态都不是我们要的结果,这时候我们没必要对这个分支进行搜索,砍掉这个子树,就是剪枝 ...
- DFS(三):八皇后问题
[例1]八皇后问题. 在一个8×8国际象棋盘上,放置8个皇后,每个皇后占一格,要求皇后间不会出现相互“攻击”的现象,即不能有两个皇后处在同一行.同一列或同一对角线上.问共有多少种不同的放置方法? (1 ...
- Tempter of the Bone——DFS(王道)
Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...
随机推荐
- 数据库——MySQL——数据类型
详细的看后面给的链接,我只是挑了一部分:http://www.runoob.com/mysql/mysql-data-types.html 在之前说了MySQL的存储引擎.它决定了表的类型,而表内存放 ...
- vue-resource+iview上传文件取消上传
vue-resource+iview上传文件取消上传 子组件: <template> <div class="upload-area-div"> <U ...
- update、commit、trancate,delete
update 用于更新表的数据,使用方式为: update table_name set column_name=值 条件 顺便一提:date数据插入更新应该使用 to_date()格式转换函数例如: ...
- Vue--- VueX基础 (Vuex结构图数据流向)1.1
Vuex基础 https://vuex.vuejs.org/zh-cn state --> view --> action -> state 多组件共享状态, 之前操作方式,由父组件 ...
- Windows环境下的RTKPlot_Qt版本编译时遇到的问题和解决方法
在使用了 RTKLIB开源包自带的 rtkplot.exe后,知道了它所具有的功能,就想着如何模仿它做出一个 demo.一开始看的是之前下载的 2.4.2版本的 RTKLIB,里面是使用 Delphi ...
- java常见面试问题.你一定会预见到。
1判断一个char字符是不是数字:Character.isDigit(char).是数字返回true,反之返回false. 2字符串的toCharArray() 把字符串转换为字符数组.返回char[ ...
- 记js里codePointAt()方法返回的结果的含义。
经过<字符串的扩展>和<字符编码的那些事>这两篇文章的阅读,大概了解js里codePointAt方法返回结果的含义. var str='
- [转]不让iTunes备份到c盘
很多人现在的C盘都是空间不大的SSD硬盘,ITUNES备份老是占越来越大的空间,不如动手把它改成其它盘好了.下面7个步骤教你转移备份. 1.需要一个小工具:Juction.exe,如果你已经是WIN7 ...
- Hadoop(8)-HDFS的读写数据流程以及机架感知
1. HDFS的写数据流程 1.客户端通过fs模块向NameNode申请文件上传,NameNode检查请求是否合法,如用户权限,目标文件是否已存在,父目录是否存在等等 2.NameNode返回是否可以 ...
- python应用:主题分类(gensim lda)
安装第三方包:gensim 首先,执行去停词操作(去除与主题无关的词) #-*-coding:utf8-*- import jieba def stopwordslist(filepath): sto ...