TZOJ 1221 Tempter of the Bone(回溯+剪枝)
描述
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(回溯+剪枝)的更多相关文章
- HDU1010 Tempter of the Bone(回溯 + 剪枝)
本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398734 题意: 输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D ...
- HDU1010:Tempter of the Bone(dfs+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...
- hdu 1010 Tempter of the Bone 奇偶剪枝
如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- hdu 1010 Tempter of the Bone 深搜+剪枝
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
随机推荐
- python 阿狸的进阶之路(7)
面向对象 转自林海峰的博客 http://www.cnblogs.com/linhaifeng/articles/6182264.html 面向对象的理解: 将数据分类,比如学生类.数据有关的函数, ...
- js 选项卡制作
知识回顾,制作JS选项卡,仅供参考 html代码: <!DOCTYPE html> <html lang="en"> <head> <me ...
- vue:再vue-cli项目中使用window以及调用window上的方法
一: 1:在main.js中 Vue.prototype.myfunction = function() {/*你的自定义Vue方法*/} 2:在mounted(或其他生命周期中) 或者 method ...
- cxgrid合并值相同的某列
设置 cxGrid 的某列的 CellMerging 属性可使这一列相同值的单元格合并. 1)cxGridDBTableViewColumn1.Options.CellMerging:=true 2 ...
- .NET(C#) win Form窗体的常用属性以及事件
使用.Net编写Windows程序,对于窗体控制常见项目 属性:1.让窗体在启动时在指定位置出现 form1.StartPosition Manual CenterScreen WindowsDefa ...
- Linux下MySQL5.7.18二进制包安装(手动添加配置文件my_default.cnf)
本文出处:http://www.cnblogs.com/wy123/p/6815049.html 最新在学习MySQL,纯新手,对Linux了解的也不多,因为是下载的最新版的MySQL(MySQL5. ...
- JAVAWEB 一一 Hibernate(框架)
实体类关联数据库字段,操作实体类,HQL语句对数据结构CRUD) 引入jar包 配置文件 hibernate.cfg.xml User.hbm.xml <?xml version="1 ...
- hive sql 效率提升
转 : http://www.cnblogs.com/xd502djj/p/3799432.html hive的查询注意事项以及优化总结 . Hive是将符合SQL语法的字符串解析生成可以在Hado ...
- 上任com的发布流程
参考:https://blog.csdn.net/qq_19674905/article/details/80268815 首先把本地代码提交到远程自己的git分支,然后merge request到m ...
- ABAP 编程
ABAP Programming Language 的内容主要有: 1.数据类型与数据对象 2.内表和内表结构(Internal Table) 3.数据流控制语句 4.模块化(Modularizati ...