【剪枝】HDU 1010——tempter of the bone
来源:点击打开链接
看上去数据规模很小,但是必须要剪枝,否则直接爆TLE。
通过这个题可以练习奇偶剪枝。
另外:还有一个优化方式,如果所有步数走完了门还没关,则直接返回结果"NO".
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std; int n,m,tarstep;
int tari,tarj;
int si,sj;
char map[10][10];
int dir[4][2]={0,1,0,-1,1,0,-1,0};
int ok=0; void dfs(int si,int sj,int step)
{
int temp;
if(si>n || sj>m || si<=0 || sj<=0)
return;
if(step==tarstep && si==tari && sj==tarj)
ok=1;
if(ok==1)
return;
//奇偶剪枝
temp=(tarstep-step)-abs(si-tari)-abs(sj-tarj);
if(temp<0 || temp&1)
return;
for(int i=0;i<4;i++)
{
if(map[si+dir[i][0]][sj+dir[i][1]]!='X')
{
map[si+dir[i][0]][sj+dir[i][1]]='X';
dfs(si+dir[i][0],sj+dir[i][1],step+1);
map[si+dir[i][0]][sj+dir[i][1]]='.';
}
}
return ;
} int main()
{
while(cin>>n>>m>>tarstep)
{
if(n==0 && m==0 && tarstep==0)
break;
int wall=0;
for(int i=1;i<=n;i++) //下标从1开始
{
for(int j=1;j<=m;j++)
{
cin>>map[i][j];
if(map[i][j]=='S')
{
si=i;
sj=j;
}
else if(map[i][j]=='D')
{
tari=i;
tarj=j;
}
else if(map[i][j]=='X')
wall++;
} }
if(n*m-wall<tarstep) //剪枝,如果能走的空地走完门没开
{
cout<<"NO"<<endl;
continue;
}
ok=0;
map[si][sj]='X'; //初始化破坏掉
dfs(si,sj,0);
if(ok==0)
{
cout<<"NO"<<endl;
}
else
{
cout<<"YES"<<endl;
} }
return 0;
}
【剪枝】HDU 1010——tempter of the bone的更多相关文章
- HDU 1010 Tempter of the Bone --- DFS
HDU 1010 题目大意:给定你起点S,和终点D,X为墙不可走,问你是否能在 T 时刻恰好到达终点D. 参考: 奇偶剪枝 奇偶剪枝简单解释: 在一个只能往X.Y方向走的方格上,从起点到终点的最短步数 ...
- 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 奇偶剪枝
如果所给的时间(步数) t 小于最短步数path,那么一定走不到. 若满足t>path.但是如果能在恰好 t 步的时候,走到出口处.那么(t-path)必须是二的倍数. 关于第二种方案的解释 ...
- 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 ...
- hdu 1010 Tempter of the Bone 深搜+剪枝
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 ...
- 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 分类: Translation Mode 2014-08-04 16:11 82人阅读 评论(0) 收藏
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU 1010 Tempter of the Bone(深度+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 题意:就是给出了一个迷宫,小狗必须经过指定的步数到达出口,并且每个格子只能走一次. 首先先来介绍一下奇偶性 ...
随机推荐
- 从Clarifai的估值聊聊深度学习
从Clarifai的估值聊聊深度学习 [转载请注明出处] 前几天和 Ayden @叶瀚中 聊天时,提到了 www.clarifai.com 这家公司. 此前,我已经从各方消息中听说过创始人Matt Z ...
- jquery mobile script
http://blog.csdn.net/lyatzhongkong/article/details/6969913 http://book.51cto.com/art/201209/355980.h ...
- 对CURL的一些研究
http://www.kuqin.com/article/23candcplusplus/586014.html 前两天看到有人求客户端socket 发HTTP包的代码,受flw版主启发找了一些per ...
- JAVA客户端API调用memcached两种方式
1. memcached client for java客户端API:memcached client for java 引入jar包:java-memcached-2.6.2.jar package ...
- IE10与IMG图片PNG显示不了 WP中的WebBrowser中无法查看PNG格式的图片
在IE10下,IMG的图片不能是PNG格式的,PNG格式显示不了,JPG显示就可以
- CentOS镜像163更新源
首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-B ...
- Android 风格化的 Toggle Buttons
Android到默认UI比iOS到默认UI在美观程度上还是有一定到差距的,我们希望能够美化UI,并且替换掉系统默认的UI风格,使得程序在使用这些UI的时候都默认使用我们自定义到UI.本文以Toggle ...
- keytool 错误: java.io.FileNotFoundException: 拒绝访问
keytool 错误: java.io.FileNotFoundException: 拒绝访问 打开命令行,切换到D:\AndroidStudioProjects\MyApplication\app目 ...
- scaleform mobile sdk for android 多点触摸 修正
修正 scaleform 的多点触控 (随手一记 给后来的人做个参考) scaleform 版本号 4.2.24 (估计这就是最后一个 移动版的版本了,万年没有更新了) 开始 一直以为 scalefo ...
- 利用switch case判断是今天的第多少天
static void Main(string[] args) { while (true) { int m1 ...