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. Android高手速成--第三部分 优秀项目

    主要介绍那些Android还不错的完整项目,目前包含的项目主要依据是项目有意思或项目分层规范比较好.Linux项目地址:https://github.com/torvalds/linuxAndroid ...

  2. [Python] 利用Django进行Web开发系列(一)

    1 写在前面 在没有接触互联网这个行业的时候,我就一直很好奇网站是怎么构建的.现在虽然从事互联网相关的工作,但是也一直没有接触过Web开发之类的东西,但是兴趣终归还是要有的,而且是需要自己动手去实践的 ...

  3. PHP中global与$GLOBALS['']的区别

    +++ 探讨(一)+++++++++++++++++++++++++++++++++++++++ 很多人都认为global和$GLOBALS[]只是写法上面的差别,其实不然. 根据官方的解释是 $GL ...

  4. 【Android学习】解决Eclipse AVD打开慢的问题

    1.创建的时候勾选“Snapshot” 2.之后Start时候勾选对应的.

  5. C和指针 第六章 指针6.2 6.3字符串中查找的两个版本

    int find_char(char **strings, char ch) { char *string; while ((string = *strings++) != NULL) { while ...

  6. angularjs 笔记(1) -- 引导

    首先: 1,引入angularJS文件,<script type="text/javascript" src="angularjs.min.js"> ...

  7. hash模块 hashlib 和hmac

    hashlib模块 用于加密相关的操作,代替md5模块和sha模块,主要提供SHA1,SHA224,SSHA256,SHA384,SHA512,MD5算法 直接看代码案例: ---------md5- ...

  8. 倒计时simple 天时分秒

    new Date()new Date(yyyy,mth,dd,hh,mm,ss); //月从0计数 .getTime()返回的是一个long型的毫秒数 毫秒转成 秒 分 时 天 <div id= ...

  9. js 的Location对象

    Location对象 location用于获取或设置窗体的URL,并且可以用于解析URL. 语法: location.[属性|方法] location对象属性图示: location 对象属性: ha ...

  10. js脚本语言

    alert(): 输出 字符串 转成整数 parseint(字符串):运算符与表达式% 取余 aler(10%2)余0 逻辑运算符&&并 ||或 !非 判断条件比较运算符== 等于 ! ...