描述

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. (原)Echarts 报Uncaught Error: Initialize failed: invalid dom 根本解决

    1.循环出的Echarts出现 Uncaught Error: Initialize failed: invalid dom ,附上完美解决方案 setTimeout(function () { co ...

  2. input点击后的 默认边框去除

    转自 http://blog.sina.com.cn/s/blog_9f1cb4670102v47g.html css文件里加句话:*:focus { outline: none; } 或 input ...

  3. World Cup 996B(排队模拟)

    题意:有n个通道,按顺序每一次站一个通道,直到所站的通道没有人 分析:模拟这个过程 #include<cstdio> int main() { ]; while(~scanf(" ...

  4. 关于maven中的快照版本(snapshot)与正式版本(release)解析。

    Maven中建立的依赖管理方式基本已成为Java语言依赖管理的事实标准,Maven的替代者Gradle也基本沿用了Maven的依赖管理机制.在Maven依赖管理中,唯一标识一个依赖项是由该依赖项的三个 ...

  5. vue项目bug-Couldn’t find preset "es2015"

    在使用vue项目的时候安装了其他的插件,发现会报错 Couldn’t find preset "es2015".是因引用的插件使用了es标准,解决办法如下 npm install ...

  6. [Sphinx]全文索引Sphinx的使用配置

    -------------------------------------------------------------------------------------- 搜索分为两种: 1. 对结 ...

  7. C#调用Delphi接口(ITest = interface)

    首先创建一个delphi的DLL工程 library testintfdll; { Important note about DLL memory management: ShareMem must ...

  8. week5 03 continus loading news

    1.server-side : Rest API 2. client-side 想要持续不断的下拉获取新闻 有两种做法 一种是在UI 我们调用API 获取所有的新闻 然后在UI 拉下的时候显示新闻 其 ...

  9. springboot 引入 thymeleaf 模板

    第一步pom中: <!-- 引入 thymeleaf 模板依赖 --> <dependency> <groupId>org.springframework.boot ...

  10. windows中 git 命令使用记录

    建议国内开发安装淘宝npm镜像cnpm npm install -g cnpm --registry=https://registry.npm.taobao.org 或者每次执行安装时 npm ins ...