一、题目回顾

题目链接:Tempter of the Bone

Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

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.

 
Input
The input consists of multiple test cases.
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.

 
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
 
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
 
Sample Output
NO
YES
 
题意:小狗从点'S'出发,每秒走一步,且每个点只能经过一次,请问它能否恰好在第T秒达到点'D' 。
 
二、解题思路
  • DFS
  • 剪枝

注意,这道题目是要恰好t时间到达,并不是在t时间内到达......

第一个剪枝我们可以想到,当剩下的步数大于剩下的时间的时候,狗是不能走到的;
 
接下来我们来第二个剪枝:
我们把map的奇偶性以01编号:
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
我们发现从0走一步一定走到1,从1走一步一定走到0。
也就是说,如果当前的狗所在的坐标与D的坐标奇偶性不一样,那么狗需要走奇数步。
同理,如果狗所在坐标与D的坐标奇偶性一样,那么狗需要走偶数步数。
 
也就是说,狗的横坐标x与纵坐标y的和对2取余是它的奇偶性,D的横坐标x与纵坐标y的和对2取余是D的奇偶性。
两个奇偶性相加再对2取余,拿这个余数去与剩下时间对2取余的余数作比较即可(不同即不可能)。
 
附图:
 
三、代码

#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的更多相关文章

  1. DFS(深度优先)算法编程实践

    DFS定义 DFS(Depth-First-Search)深度优先搜索算法,是搜索算法的一种.是一种在开发爬虫早期使用较多的方法.它的目的是要达到被搜索结构的叶结点 . 特点 每次深度优先搜索的结果必 ...

  2. 拓扑排序+DFS(POJ1270)

    [日后练手](非解题) 拓扑排序+DFS(POJ1270) #include<stdio.h> #include<iostream> #include<cstdio> ...

  3. DFS(一):深度优先搜索的基本思想

    采用搜索算法解决问题时,需要构造一个表明状态特征和不同状态之间关系的数据结构,这种数据结构称为结点.不同的问题需要用不同的数据结构描述. 根据搜索问题所给定的条件,从一个结点出发,可以生成一个或多个新 ...

  4. 深度优先搜索DFS(一)

      实例一  0/1背包问题:   有n件物品,每件物品的重量为w[i],价值为c[i].现在需要选出若干件物品放入一个容量为V的背包中,使得在选入背包的物品重量和不超过容量V的前提下,让背包中的物品 ...

  5. 万能的搜索--之DFS(二)

    (一)深度优先搜索(DFS) 我们先给出深度优先的解决办法,所谓深度优先搜索,在迷宫问题里就是不撞南墙不回头,能走得深一点就尽量深一点.如果碰到了墙壁就返回前一个位置尝试其他的方向.在<啊哈!算 ...

  6. DFS(二):骑士游历问题

    在国际象棋的棋盘(8行×8列)上放置一个马,按照“马走日字”的规则,马要遍历棋盘,即到达棋盘上的每一格,并且每格只到达一次.例如,下图给出了骑士从坐标(1,5)出发,游历棋盘的一种可能情况. [例1] ...

  7. DFS(四):剪枝策略

    顾名思义,剪枝就是通过一些判断,剪掉搜索树上不必要的子树.在采用DFS算法搜索时,有时候我们会发现某个结点对应的子树的状态都不是我们要的结果,这时候我们没必要对这个分支进行搜索,砍掉这个子树,就是剪枝 ...

  8. DFS(三):八皇后问题

    [例1]八皇后问题. 在一个8×8国际象棋盘上,放置8个皇后,每个皇后占一格,要求皇后间不会出现相互“攻击”的现象,即不能有两个皇后处在同一行.同一列或同一对角线上.问共有多少种不同的放置方法? (1 ...

  9. Tempter of the Bone——DFS(王道)

    Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, ...

随机推荐

  1. linux系统命令与常识

    之前短期学过linux,用到时才发现已经忘得一干二净了. 现在对学过的和了解到的做一个总结: 先明确一些使用工具: winscp : WinSCP是一个Windows环境下使用SSH的开源图形化SFT ...

  2. Django-rest-framework(四)router

    在上一节viewsets中,我们提到了route的方式使用,在这一节,我们将仔细介绍drf 的router机制.简单来说,router的作用就是将viewset对象的url映射关系提取出来. 简单使用 ...

  3. iOS | 地图定位

    在IOS开发中,最常见的功能之一就是地图定位功能,不单单是百度地图,高德地图等专业的地图导航软件,还有美团,咕咚等一些美食购物类和运动类也需要这样的功能,所以学会这项技能是一名IOS开发工程师必须的. ...

  4. 工具类(设置控件 frame) - iOS

    为了便于日常开发效率,因此创建了一些小的工具类便于使用. 具体 code 如下: 声明: #import <UIKit/UIKit.h> @interface UIView (Frame) ...

  5. iOS 让视图UIView单独显示某一侧的边框线

    iOS 让视图UIView 单独显示某一侧的边框线   有时候需要让view显示某一侧的边框线,这时设置layer的border是达不到效果的.在网上查阅资料发现有一个投机取巧的办法,原理是给view ...

  6. vue入门:实现图片点击切换

    1.实现功能 2.目录结构 3.代码 <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  7. web前端逻辑计算,血的教训

    在web前端进行页面开发的过程中,难免的遇到逻辑问题,这不是什么大问题,既然走上IT条黑道,那小伙伴们的逻辑推理能力及逻辑计算能力是不会有太大问题的. 然而,有的逻辑计算,就算你逻辑计算能力超强,也不 ...

  8. Laravel -- Blade模板

    {{--流程控制--}} @if($name == '1') this is 1 @elseif($name == '2') this.is 2 @else who am i? @endif @for ...

  9. 对bluebird的理解

    前言 Promise:把原来的回调写法分离出来,在异步操作执行完后,用链式调用的方式执行回调函数. 在公众号的开发里面用的const Promise = require('bluebird');con ...

  10. P3388 【模板】割点

    题目背景 割点 题目描述 给出一个n个点,m条边的无向图,求图的割点. 输入输出格式 输入格式: 第一行输入n,m 下面m行每行输入x,y表示x到y有一条边 输出格式: 第一行输出割点个数 第二行按照 ...