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 ...
随机推荐
- Linux tcpdump命令使用方法
tcpdump是Linux上常用的抓包命令,用于截取网络分组并输出分组内容,常用于网络问题分析和排查. tcpdump语法 tcpdump [-i 接口] [-nn] [-w 文件名] [-c 次数] ...
- 使用Node.JS监听文件夹变化
使用Node.JS监听文件夹改变有许多应用场合,比如: 构建自动编绎工具 当源文件改变时,自动运行build过程,比如当你写CoffeeScript文件或SASS CSS文件时,保存之后可即时生成对应 ...
- MyBatis对入参对象的属性空判断
<!-- 查询学生list,like姓名 --> <select id="getStudentListLikeName" parameterType=&q ...
- 命令行下IIS的配置脚本Adsutil.vbs
命令行下IIS的配置脚本Adsutil.vbs 2009-08-20 12:26:52 www.hackbase.com 来源:Jackal's Blog Jackal's Blog文件存在于:C ...
- Oracle 连接数据库
使用的DLL:Oracle.ManagedDataAccess Bug:OracleInternal.Common.ProviderConfig的类型初始值设定项引发异常 App.config的更改才 ...
- NYOJ44-子串和-(dp||思维)
题目描述: 给定一整型数列{a1,a2...,an},找出连续非空子串{ax,ax+1,...,ay},使得该子序列的和最大,其中,1<=x<=y<=n. 输入描述: 第一行是一个整 ...
- mysql读写分离 主从同步
MySQL主从复制与读写分离的实现 转载 2013年01月17日 18:20:12 MySQL主从复制与读写分离 MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy) ...
- React开发调试工具--react-developer-tools
1. 首先,下载react-developer-tools开发调试工具插件. 因为谷歌插件下载需要FQ,这里提供一个本地资源:https://www.crx4chrome.com/crx/3068/ ...
- k8s 创建deployment流程
pod 创建流程https://blog.csdn.net/yan234280533/article/details/72567261 api server -> etcd -> cont ...
- div下面多个a标签的点击事件,并且获取a的属性
$('.fensiselect').on('click','a',function(){ var id= $(this).attr('fanid'); alert(id) })