描述

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.

输入

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.

输出

For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.

样例输入

4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0

样例输出

NO
YES

题意

给你一个n*m大小的矩阵,判断老鼠能否刚好在T步后到达出口(‘.’是只能经过一次的路,K步后出口打开)

题解

一个经典的回溯题,暴力写完后提交TLE,后来发现需要剪枝

最短路minstep=abs(s.x-e.x)+abs(s.y-e.y),最长路maxstep=n*m-1;

可以想到一个简单的剪枝,如果老鼠从起点开始,最短路minstep>t直接NO,或者最长路maxstep<t直接NO,写完后提交,又TLE,还要剪枝

通过多次画图可以发现,

0,1,0,1,0

1,0,1,0,1

0,1,0,1,0

1,0,1,0,1

如果起点和终点不同(0->1)(1->0),那么,要经过奇数步才能到达终点

如果起点和终点相同(0->0)(1->1),那么,需经过偶数步才能到达终点

那就很明显了,需要奇偶剪枝,(t-minstep)%2==1直接NO,在回溯的时候也可以用,大大降低了不必要的运算

代码

 #include<bits/stdc++.h>
using namespace std;
char G[][];
inline int Abs(int a){return a>?a:-a;}
struct p
{
int x,y;
}s,e;
int dx[]={,,,-};
int dy[]={,-,,};
int n,m,t,flag,minstep;
void dfs(int x,int y,int k)
{
if(x==e.x&&y==e.y&&k==)flag=;
minstep=Abs(x-e.x)+Abs(y-e.y);
if(minstep>k||(k-minstep)%)return;
if(!flag)
{
for(int i=;i<;++i)
{
int xx=x+dx[i];
int yy=y+dy[i];
if(xx>=&&xx<n&&yy>=&&yy<m&&G[xx][yy]!='X')
{
G[xx][yy]='X';
dfs(xx,yy,k-);
G[xx][yy]='.';
}
}
}
}
int main()
{
while(scanf("%d%d%d",&n,&m,&t)!=EOF,n||m||t)
{
flag=;
for(int i=;i<n;++i)
{
scanf("%s",G[i]);
for(int j=;j<m;++j)
{
if(G[i][j]=='S')
s.x=i,s.y=j;
else if(G[i][j]=='D')
e.x=i,e.y=j;
}
}
minstep=Abs(s.x-e.x)+Abs(s.y-e.y);
if(n*m-<t||minstep>t||(t-minstep)%){printf("NO\n");continue;}
G[s.x][s.y]='X';
dfs(s.x,s.y,t);
printf("%s\n",flag?"YES":"NO");
}
return ;
}

TZOJ 1221 Tempter of the Bone(回溯+剪枝)的更多相关文章

  1. HDU1010 Tempter of the Bone(回溯 + 剪枝)

    本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398734 题意: 输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D ...

  2. HDU1010:Tempter of the Bone(dfs+剪枝)

    http://acm.hdu.edu.cn/showproblem.php?pid=1010   //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...

  3. hdu 1010 Tempter of the Bone 奇偶剪枝

      如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...

  4. Tempter of the Bone dfs+剪枝

    The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it u ...

  5. B - Tempter of the Bone(DFS+剪枝)

    The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it u ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. hdu 1010 Tempter of the Bone 深搜+剪枝

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

随机推荐

  1. js页面百分比缩放

    <script> var docEl = document.documentElement, resizeEvt = 'orientationchange' in window ? 'or ...

  2. day35-常见内置模块四(logging模块)

    一.函数式简单配置(低配) 1.只能在屏幕上显示,或者写入文件,不能同时进行 import logging logging.debug('调试') logging.info('正常运行') loggi ...

  3. 1005 继续(3n+1)猜想 (25 分)

    1005 继续(3n+1)猜想 (25)(25 分) - 过期汽水的博客 - CSDN博客https://blog.csdn.net/qq_40167974/article/details/80739 ...

  4. How to Pronounce the Months of the Year

    How to Pronounce the Months of the Year Share Tweet Share Tagged With: Most Popular Some of the mont ...

  5. Android RxJava 2 的用法 just 、from、map、subscribe、flatmap、Flowable、Function、Consumer ...【转】

    先简单说说RxJava的用途与价值 原文出处:Android RxJava 2 的用法 用途: 异步 (也就是开线程跳转) 价值: 面对复杂的逻辑,它依然 简洁 ,代码 易读 RxJava2 与 Rx ...

  6. C++ : 窗口变化相关消息 OnSize、OnSizing和OnGetMinMaxInfo,onsizeonsizing

    个消息分别是:WM_SIZE.WM_SIZING.WM_GETMINMAXINFO:分别对应相应的处理函数:OnSize.OnSizing.OnGetMinMaxInfo. 当窗口大小发生变化时,响应 ...

  7. 配置 SQL Server 2008 Email 发送以及 Job 的 Notification通知功能

    SQL Server 2008配置邮件的过程就不写了,网上的案例太多了. http://www.cnblogs.com/woodytu/p/5154526.html 这个案例就不错. 主要写下配置完后 ...

  8. ios instancetype 和 id 的异同

    1.0 相同点:都可以作为方法的返回类型 2.0 不同点: a.instancetype 可以返回和方法所在类相同类型的对象   id 只能返回未知类型的对象 b. instancetype 只能作为 ...

  9. 吴裕雄 27-MySQL 元数据

    你可能想知道MySQL以下三种信息:查询结果信息: SELECT, UPDATE 或 DELETE语句影响的记录数.数据库和数据表的信息: 包含了数据库及数据表的结构信息.MySQL服务器信息: 包含 ...

  10. 学JS的心路历程-函式(四)apply、call

    从上一篇可以知道,不同的函式呼叫会造成this的不同,但我们能不能在呼叫时候明确指定呢? 当然可以.会有这个想法是因为往往在执行某支函式时想要用回呼函式(mizumisushi),但发现this总是显 ...