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, ...
随机推荐
- 学习Node.js知识小结
什么是Node.js 官方解释:Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node.js使用了一个事件驱动.非阻塞式I/O的模型( Node.js的特性 ...
- Kong Api 初体验
请查看原文: https://www.fangzhipeng.com/nginx/kong/2016/07/11/kong-api-gateway/ Kong是一个可扩展的开源API层(也称为API网 ...
- 关于SQLNET.AUTHENTICATION_SERVICES= (NTS) 的解释
原文转自:http://www.360doc.com/content/12/0207/12/3446769_184740592.shtml 标题所代表的意思为 使用操作系统本地验证,一般不 ...
- iOS | FMDB快速上手
任何的开发都或多或少的接触到数据库,而在IOS中一般使用的是SQLite数据库,这是一个轻量功能较为不错的数据库.而现在用到比较多的第三方数据库操作框架就是FMDB.废话不多说,相信查找到这篇文章的都 ...
- 小A点菜
题目背景 uim神犇拿到了uoi的ra(镭牌)后,立刻拉着基友小A到了一家--餐馆,很低端的那种. uim指着墙上的价目表(太低级了没有菜单),说:"随便点". 题目描述 不过ui ...
- hdu_2588_GCD
The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the ...
- ABAP术语-ISO (International Organization for Standardization)
ISO (International Organization for Standardization) 原文:http://www.cnblogs.com/qiangsheng/archive/20 ...
- 基于OMAPL:Linux3.3内核的编译
基于OMAPL:Linux3.3内核的编译 OMAPL对应3个版本的linux源代码,分别是:Linux-3.3.Linux-2.6.37.Linux2.6.33,这里的差距在于Linux2,缺少SY ...
- C语言学习记录_2019.02.02
变量在第一次被使用之前应该赋初值 scanf(“%d”,&price); scanf(“price%d %d”,&price); scanf中的东西一定是要输入的东西. 定义常量:c ...
- POJ1236 tarjan
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19613 Accepted: 77 ...