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, ...
随机推荐
- vue使用v-for循环,动态修改element-ui的el-switch
在使用element-ui的el-switch中,因为要用v-for循环,一直没有成功,后来仔细查看文档,发现可以这样写 <el-switch v-for="(item, key) i ...
- 在Win7虚拟机下搭建Hadoop2.6.0+Spark1.4.0单机环境
Hadoop的安装和配置可以参考我之前的文章:在Win7虚拟机下搭建Hadoop2.6.0伪分布式环境. 本篇介绍如何在Hadoop2.6.0基础上搭建spark1.4.0单机环境. 1. 软件准备 ...
- sqlServer2014安装说明(windows7 64位)
SqlServer2014安装说明(windows7 64位) 地址:https://www.microsoft.com/zh-cn/download/details.aspx?id=42299 1, ...
- BZOJ2844: albus就是要第一个出场(线性基)
Time Limit: 6 Sec Memory Limit: 128 MBSubmit: 2054 Solved: 850[Submit][Status][Discuss] Descriptio ...
- 三种for循环遍历
import java.util.ArrayList;import java.util.Iterator;import java.util.List; public class For{ publi ...
- mongodb的高级查询
db的帮助文档 输入:db.help(); db.AddUser(username,password[, readOnly=false]) 添加用户 db.auth(usrename,passwor ...
- 微信中h5页面用window.history.go(-1)返回上一页页面不会重新加载问题
问题描述: 在实际开发中遇到这样一个问题,业务需求涉及到返回上一页问题,第一时间想到了window.history.go(-1)方法,这样做本身没有任何问题,但是在微信中,安卓手机还好返回上一页页面会 ...
- Hive初识(三)
根据用户的需求创建视图.可以将任何结果集数据保存为一个视图.视图在Hive的用法和SQL视图用法相同.它是一个标准的RDBMS概念.我们可以在视图上执行所有DML操作. 创建一个试图 可以创建一个试图 ...
- ruby 比较符号==, ===, eql?, equal?
“==” 最常见的相等性判断 “==” 使用最频繁,它通常用于对象的值相等性(语义相等)判断,在 Object 的方法定义中,“==” 比较两个对象的 object_id 是否一致,通常子类都会重写覆 ...
- Python tips(
(此文是在实际工程中遇到的一些小问题,给予解决和整理.解决方法大多来自网上零散的文章.有一个系统化的Python问题解决方案,来自<Python 3 学习笔记>雨痕著,其中对Python的 ...