一、题目回顾

题目链接: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. Java基础随笔2

    各类运算符: 算数运算符::+,-,*,/,%,++,-- ++:自增 --:自减 单独使用的时候,++或者--无论是放在变量的前面还是后面,结果是一样的. 参与操作的时候: 如果++或者--在变量的 ...

  2. Java基础——XML复习

    XML                 SGML : 标准通用置标语言    Standard Generailzed    Markup Language XML                   ...

  3. linux下重新启动oracle

    第一步.以Oracle帐户进入Linux系统 第二步.执行以下命令查看数据库监听器的状况: lsnrctl status 或者查看数据库端口是否被监听(默认1521) netstat -ano | g ...

  4. IPC进程间通信---信号量

    信号量 信号量:信号量是一个计数器,常用于处理进程或线程的同步问题,特别是对于临界资源访问的同步.临界资源可以 理解为在某一时刻只能由一个进程或线程操作的资源,这里的资源可以是一段代码.一个变量或某种 ...

  5. jsonp跨域请求360数据乱码解决办法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  6. python 用selenuim判断页面是否全部加载完成,并且加上最大时长,超过时长报错

    STR_READY_STATE = '' time_start = time.time() while STR_READY_STATE != 'complete': time.sleep(0.001) ...

  7. Spirng+SpringMVC+Mybatis(一)

    实习之后都是在别人搭配好环境的情况下进行一些业务的编写,脑袋已经不记得如何搭建一个ssm项目的,所以周末有空补了一下. 首先新建一个test数据库,并且在里面插入三条数据.如图下 编写一个User B ...

  8. Linux入门-第三周

    1.总结vim命令行模式常见快捷方式,以及vim查找,替换的方法 vim [options] [file ..] +# 打开文件后,让光标处于第#行的行首,(默认行尾) 举例vim +10 /etc/ ...

  9. JAVAOOP多线程

    进程每个独立运行的任务对应一个进程,每个进程可以产生多个线程 特点:1,进程是系统运行程序的基本单位 2,每一个进程都有自己独立的一块内存空间,一组系统资源 3,每一个进程的内部数据和状态都是完全独立 ...

  10. JS数组&对象遍历

    遍历的总结,经常用到的,希望帮助你我成长. JS数组遍历: 1,普通for循环 var arr = [1,2,3,4,9]; for ( var i = 0; i <arr.length; i+ ...