Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 110290    Accepted Submission(s): 29967

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; or
'.': 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
 
Author
ZHANG, Zheng
 
Source
题意:
从地图中的S点经过T时间是否能够恰好到达D点;
代码:
 //很明显是一道dfs,但普通的dfs会超时,看了题解才明白原来可以奇偶剪枝。从起点到终点的距离如果是奇数t也是奇数才能到达,
//从起点到终点的距离如果是偶数t是偶数才能到达。从起点到终点的最短距离是两点的坐标之差,如果不走这条最短路,只有多走的步数是偶数
//步时才能到达终点。因此可以排除很多情况。可以自己画图看看。
//如果地图上可走的点少于t也不行。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n,m,t;
int ans,tim;
int dir[][]={,,-,,,,,-};
bool vis[][];
char map[][];
void dfs(int sx,int sy)
{
if(tim>t)
return;
if(map[sx][sy]=='D')
{
if(tim==t)
ans=;
return;
}
for(int i=;i<;i++)
{
int x=sx+dir[i][],y=sy+dir[i][];
if(x<||x>=n||y<||y>=m) continue;
if(vis[x][y]) continue;
if(map[x][y]=='X') continue;
vis[x][y]=;
tim++;
dfs(x,y);
vis[x][y]=;
tim--;
if(ans==) return;
}
}
int main()
{
int sx,sy,ex,ey;
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
if(n==&&m==&&t==) break;
int cnt=;
for(int i=;i<n;i++)
{
scanf("%s",map[i]);
for(int j=;j<m;j++){
if(map[i][j]=='S')
{
sx=i;sy=j;
}
if(map[i][j]=='D')
{
ex=i;ey=j;
}
if(map[i][j]=='.')
cnt++;
}
}
int tem1=fabs(sx+sy-ex-ey);
ans=;
if(tem1%==t%&&cnt+>=t){
memset(vis,,sizeof(vis));
vis[sx][sy]=;
tim=;
dfs(sx,sy);
}
if(ans==) printf("YES\n");
else printf("NO\n");
}
return ;
}

HDU1010 DFS+剪枝的更多相关文章

  1. hdu-1010 dfs+剪枝

    思路: 剪枝的思路参考博客:http://www.cnblogs.com/zibuyu/archive/2012/08/17/2644396.html  在其基础之上有所改进 题意可以给抽象成给出一个 ...

  2. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  3. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  4. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  5. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  6. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  7. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  9. LA 6476 Outpost Navigation (DFS+剪枝)

    题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...

随机推荐

  1. jquery numberbox赋值

    numberbox不能使用$('#id').val( '');只能使用$('#id').numberbox('setValue','');

  2. can't debug windows service in win7 64bit

    if encount below error: Solution: run the command “vsdiag_regwcf.exe -i” as admin in C:\Program File ...

  3. 如何修改 Total commander 配置文件的路径

    在官方主页 http://www.ghisler.com/tools.htm#other 上提供了一个名为 INIreloc.exe  的小程序,它可以变更ini文件的保存位置.

  4. Javascript实现图片预加载【回调函数,多张图片】

    使用JS实现一组图片动画效果或者使用HTML5 Canvas渲染一系列图片等案例中,需要图片全部加载完成方可运行动画效果.此时程序中就会涉及多张图片预加载代码.当接二连三的案例中都涉及图片预加载时,就 ...

  5. javascript数据结构-优先队列

    这里之所以扩充一个 有限队列 是因为,生活使用中队列通常会附加优先级,比如排队买票,一般老人和军人等会有优先权限. 实现:继承上篇的 普通队列实现.这里用一种方法,入队的时候,进行排序插入到指定位置, ...

  6. JVM相关参数的采集

    1.以-jar方式启动jar包: java -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.port=40100 ...

  7. NOIp2014 解题报告

    有史以来第一届面向社会征题的NOIp结束了.最开始以为面向社会征题会很难,但是这是我参加的最水的一次NOIp了. 由于停了两月的课,所以现在正在补文化科目就没时间打代码了.所以所有的题目就均不给出代码 ...

  8. Palindrome Number

    Determine whether an integer is a palindrome. Do this without extra space. public class Solution { p ...

  9. poj 1270(toposort)

    http://poj.org/problem?id=1270 题意:给一个字符串,然后再给你一些规则,要你把所有的情况都按照字典序进行输出. 思路:很明显这肯定要用到拓扑排序,当然看到discuss里 ...

  10. Monkeyrunner 常用按键

    MonkeyRunner常用的按键介绍 Home键:KEYCOD_HOME   Back键:KEYCODE_BACK  send键:KEYCODE_CALL  end键:KEYCODE_ENDCALL ...