解题心得:

1、注意审题,此题是在规定的时间达到规定的地点,不能早到也不能晚到。并不是最简单的dfs

2、在规定时间达到规定的地点有几个剪枝:

一、公式:所需的步骤 - x相差行 - y相差列 = 偶数。(这个解释很简单,无论怎么走,都可以把走的总路程分解到x方向和y方向,哪怕反向走,走回来后的步骤+反向走的步骤也一定是偶数,假设运用正交分解走到了终点(上面公式),但是步骤没走够,可以以最终的目标点为起点任选两格来回走消耗步骤,但是减去正交分解的步数后若是奇数,那么在终点来回走将步数消耗完之后必然会走出去而不能停留在终点。)

二、总的格数减去墙数一定大于等于所需的步骤

三、当时间超出了所需的时间时要return,并且消除标记。

题目:

Tempter of the Bone

Time Limit :1s

Memory limit :32M

Accepted Submit :305

Total Submit :1026

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.

S

…D

0 0 0

Sample Output

NO

YES

#include<stdio.h>
#include<cstring>
#include<iostream>
using namespace std;
char maps[10][10];
int n,m,t,cnt//记录步骤;
int use[10][10],dir[4][2]={0,1,0,-1,1,0,-1,0};
bool flag//标记是否在规定的时间点找到目标;
struct node
{
int x,y;
}aim,now;//记录出口和启动点
int abs(int num)
{
if(num < 0)
return 0-num;
else
return num;
}
void dfs(int x,int y,int cnt)
{
int i,temp;
if(x == aim.x && y == aim.y && t == cnt)
{
flag = true;
printf("YES\n");
return;
}
temp = abs(t-cnt) - (aim.x - x) - (aim.y - y);//这很重要,一定要注意,在时间超出之后要return,。。。。哎!
if(temp<0 || temp%2)
return;
if(x<0 || y<0 || x>=n || y>=m)
return;
for(i=0;i<4;i++)
{ if(use[x+dir[i][0]][y+dir[i][1]] != 1 && maps[x+dir[i][0]][y+dir[i][1]] != 'X')
{
use[x+dir[i][0]][y+dir[i][1]] = 1;
dfs(x+dir[i][0],y+dir[i][1],cnt+1);
if(flag)
return;
use[x+dir[i][0]][y+dir[i][1]] = 0;
}
}
return;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
int d = 0;;
if(n == 0 && m == 0 && t == 0)
break;
cnt = 0;
memset(use,0,sizeof(use));
for(int i=0;i<n;i++)
scanf("%s",maps[i]);
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(maps[i][j] == 'D')
{
aim.x = i;
aim.y = j;
}
if(maps[i][j] == 'S')
{
now.x = i;
now.y = j;
}
if(maps[i][j] == 'X')
d++;
}
}
if(m*n - d < t)
{
printf("NO\n");
continue;
}
flag = false;
use[now.x][now.y] = 1;
dfs(now.x,now.y,cnt);
if(!flag)
printf("NO\n");
}
}

DFS:Tempter of the Bone (规定时间达到规定地点)的更多相关文章

  1. DFS Tempter of the Bone

    http://acm.hdu.edu.cn/showproblem.php?pid=1010 用到了奇偶剪枝: 0 1 0 1 1 0 1 0          如图,设起点为s,终点为e,s-> ...

  2. hdu.1010.Tempter of the Bone(dfs+奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  3. hdu 1010:Tempter of the Bone(DFS + 奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. HDU 1010 Tempter of the Bone --- DFS

    HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...

  5. hdoj 1010 Tempter of the Bone【dfs查找能否在规定步数时从起点到达终点】【奇偶剪枝】

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  6. Tempter of the Bone(dfs+奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  7. Tempter of the Bone(dfs奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  8. ZOJ 2110 Tempter of the Bone(条件迷宫DFS,HDU1010)

    题意  一仅仅狗要逃离迷宫  能够往上下左右4个方向走  每走一步耗时1s  每一个格子仅仅能走一次且迷宫的门仅仅在t时刻打开一次  问狗是否有可能逃离这个迷宫 直接DFS  直道找到满足条件的路径 ...

  9. HDU 1010 Tempter of the Bone【DFS经典题+奇偶剪枝详解】

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

随机推荐

  1. 《Head First 设计模式》之策略模式——鸭子行为

    策略模式(Strategy Pattern) ——定义了算法族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化独立于使用算法的客户. (每个功能的多种实现成为一个算法族,这些算法族被分别封装 ...

  2. Filter过滤器,xml配置与页面不乱码整理

    1.xml配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...

  3. 观察者模式(Observe Pattern)

    观察者模式: 当对象存在一对多关系时,使用观察者模式(Observe Pattern).例如:当一个对象被修改时,会通知它的依赖对象. 介绍: 1.意图:定义对象的一种一对多的依赖关系,当一个对象的状 ...

  4. https验证新发现-老知识

    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier); 可以设置https全局的域名校验规则 HttpsURLConnecti ...

  5. 【Oracle】曾经的Oracle学习笔记(4-7)多表联合查询,子查询,动态条件查询

    一.多表联合查询 二.子查询 三.动态条件查询 LESSON 4 Displaying Data from Multiple Tables------------------------------- ...

  6. 人工智能背景下的 Office 365 现状和发展趋势

    谈论人工智能是让人兴奋的,因为它具有让人兴奋的两大特征 —— 每个人都似乎知道一点并且以知道一点为荣,但又好像没多少人能真正讲的明白.毫无疑问,我也仅仅是知道一点点,这一篇文章试图想通过比较接地气的方 ...

  7. DB2数据库常用语句

    1.快速清空大量数据表数据,但是还原不了 alter table rm_customer activate not logged initially with empty table2.大量导出表语句 ...

  8. python 函数学习之sys.argv[1]

    一.sys 模块 sys是Python的一个「标准库」,也就是官方出的「模块」,是「System」的简写,封装了一些系统的信息和接口. 官方的文档参考:https://docs.python.org/ ...

  9. LeetCode Search Insert Position (二分查找)

    题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. class Solution { public: i ...

  10. Head First HTML与CSS阅读笔记(一)

    之前写过不少前端界面,但是没有完整阅读过一本HTML与CSS的书籍,都是用到什么查什么,最近闲暇之余想巩固加深一下前端基础方面的知识,阅读了<Head First HTML与CSS>,感觉 ...