描述

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

    ELK是三个工具的集合,Elasticsearch + Logstash + Kibana,这三个工具组合形成了一套实用.易用的监控架构,很多公司利用它来搭建可视化的海量日志分析平台. 2. Logs ...

  2. dpkg卸载

    from:https://jingyan.baidu.com/article/f54ae2fc2724a71e92b849c4.html 选择 dpkg -l来查看软件的状态. 选择 dpkg -P来 ...

  3. Windows系统封装总结

    注:使用虚拟机或者实体机进行封装均可,实体机进行封装的成功率更高.虚拟机进行封装建议使用VMware,12版本.过高的版本容易造成封装失败 一.            Windows 10系统封装 1 ...

  4. Docker容器硬盘动态扩容

    扩容容器 docker容器默认的空间是10G,如果想指定默认容器的大小(在启动容器的时候指定),可以在docker配置文件里通过dm.basesize参数指定,比如 1 docker -d --sto ...

  5. mysql 授权命令

    MySQL 数据库赋予用户权限操作表   MySQL清空数据库的操作:truncate table tablename; MySQL 赋予用户权限命令的简单格式可概括为:grant 权限 on 数据库 ...

  6. ckeditor使用说明

      2015-08-17 15:42热心网友最快回答 一.使用方法:1.在页面<head>中引入ckeditor核心文件ckeditor.js<script type="t ...

  7. Celery + RabbitMq 示意图

    一直搞不清楚消息队列和任务队列是如何结合的,直到碰到了 :http://www.cnblogs.com/jijizhazha/p/8086119.html 中的图,恍然大悟,凭借自己的理解,画了这幅组 ...

  8. idea中pom文件需要添加的依赖

    <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉. --> <!-- IDEA的maven项目中,默认源代码目录下的xml等资源文件并不会在编译的时候一块 ...

  9. effective C++学习三(仅供个人学习记录,本文摘录effective C++)

    条款 3:尽量用 new 和 delete 而不用 malloc 和 free  把 new和 delete 与malloc 和 free 混在一起用也是个坏想法.对一个用 new 获取来的指针调用 ...

  10. C#中的 new Random()

    在C#中,产生随机数常用大方法是 new Random().Next(1,10)等方法. 但是仔细发现会有个问题: 看代码: ; i < ;i++ ) { Console.WriteLine(, ...