Tempter of the Bone------剪枝
看了好多别人的 代码,最终还是 感觉 这种代码的风格适合我 下面附上代码
/* 首先 应该充满信心! 先写出来 自己的程序 然后慢慢改 ,
如果是 答题思路错误的话 借鉴别人的 代码 再写
*/
/*没有剪枝 时间超限*/
#include<stdio.h>
#include<string.h>
char a[][];
int visited[][];
int s[][]={-,,,,,-,,};
int n,m,t,bx,by,cx,cy,step=,mark;
void DFS(int y,int x,int step) // 传输 进去现在 的 位置 并且传出过去 已有的 步数
{
if(y==cy&&x==cx&&step==t)
{
mark=;
}
visited[y][x]=;
for(int i=;i<;i++)
{
if(a[y+s[i][]][x+s[i][]]!='X'&&(y+s[i][])>=&&(y+s[i][])<n&&(x+s[i][])>=&&(x+s[i][])<m&&!visited[y+s[i][]][x+s[i][]]) // 不超界 并且 不是墙 而且没有访问
{
step++;
DFS(y+s[i][],x+s[i][],step);
step--;
} }
visited[y][x]=;
}
int main()
{
int i,j;
while(scanf("%d%d%d",&n,&m,&t)!=EOF)
{
if(n==&&m==&&t==)
break
getchar();
memset(visited,,sizeof(visited));
for(mark=step=i=;i<n;i++)
{
for(j=;j<m;j++)
{
scanf("%c",&a[i][j]);
if(a[i][j]=='S') // 找到进入的坐标
{
bx=j;
by=i;
}
if(a[i][j]=='D') // 找到出去的坐标
{
cx=j;
cy=i;
}
}
getchar();
}
DFS(by,bx,); // 传输 当前座标
if(mark!=)
printf("NO\n");
else
printf("YES\n");
}
return ;
}
上面的 没有剪枝 时间超限 下面开始 剪枝 .
/* 首先 应该充满信心! 先写出来 自己的程序 然后慢慢改 , 如果是 答题思路错误的话 借鉴别人的 代码 再写 */
/*
剪枝:
1: 奇偶剪枝:到终点的最短步数(就算需要绕路也没事要的奇偶) 和 到终点限定的步数 如果 奇偶性不同的话是不可能达到的 (绕路是同时多出偶数倍的步数) 1/2 的时间
2: 进去的时候 到 终点的最短路 如果大于 给定的步数 也不可能 . (也可能绕路绕超所以需要重复)
3: 可以走的格子 走完但是仍然还不够限定的步数 就不行了
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<map>
#include<cctype>
#include<limits.h>
#define leng 10
using namespace std;
char a[leng][leng];
int ex,ey,n,m,t,bx,mark,by,b[][]={,,,,,-,-,},visited[leng][leng];
void DFS(int y,int x,int step)
{
int d=t-step-abs(y-ey)-abs(x-ex); // 剩下 的步数 - 到终点的步数 小于 0 完蛋 .
if(t<step||mark==||d<||d%) // 这里三个 剪枝 . 上述公式 如果不是 偶数的话 , 就完蛋.
return;
if(a[y][x]=='D'&&t==step)
{
mark=;
}
visited[y][x]=;
for(int i=;i<;i++)
{
int tx=x+b[i][],ty=y+b[i][];
if(a[ty][tx]!='X'&&tx>=&&tx<m&&ty>=&&ty<n&&!visited[ty][tx]) // 不是 墙 不超界 没用过
{
DFS(ty,tx,step+);
}
}
visited[y][x]=;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&t),!(n==m&&m==t&&t==))
{
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
{
scanf(" %c",&a[i][j]);
if(a[i][j]=='S')
{
bx=j;
by=i;
}
if(a[i][j]=='D')
{
ex=j;
ey=i;
}
}
}
memset(visited,,sizeof(visited));
mark=;
DFS(by,bx,);
if(mark==)
printf("NO\n");
else
printf("YES\n");
}
return ;
}
Tempter of the Bone------剪枝的更多相关文章
- 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 ...
- 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 ...
- Hdu1010 Tempter of the Bone(DFS+剪枝) 2016-05-06 09:12 432人阅读 评论(0) 收藏
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- HDU1010:Tempter of the Bone(dfs+剪枝)
http://acm.hdu.edu.cn/showproblem.php?pid=1010 //题目链接 http://ycool.com/post/ymsvd2s//一个很好理解剪枝思想的博客 ...
- 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 ...
随机推荐
- 有哪些可以节省chrome内存的扩展插件?
不知道从什么时候开始,chrome浏览器就这样不知不觉的超过IE浏览器成为全球第一大浏览器.我们在赞赏chrome浏览器流畅的速度时,更多的是对其chrome插件功能的赞赏.但是我们也发现了一个致命的 ...
- Python基础(六) 基础文件操作
今天学习python下对文件的基础操作,主要从open函数.File对象的属性.文件定位.简单操作.举例说明几个步骤开始学习,下面开始进入今天的主题: 一.open函数介绍 open函数主要是打开一个 ...
- Shiro_认证思路分析
[认证] 也就是登录. 1.获取当前的subject,调用SecurityUtils.getSubject() 2.测试当前的用户是否已经被认证,即是否登录.调用subject的isAuthentic ...
- [luoguP1196] 银河英雄传说(并查集)
传送门 记录 up[x] 表示 x 上方有多少个 all[x] 表示当前连通的有多少个 find 的时候 和 合并的时候 更新一下即可 ——代码 #include <cstdio> #in ...
- Cocoa -- 添加和移除开机启动项
一 写plist到~/Library/LaunchAgents/ 目录下 // 配置开机默认启动 -(void)installDaemon{ NSString* launchFolder = [NSS ...
- NOIP2010 提高组合集
NOIP 2010 提高组合集 T1 机器翻译 模拟题,用一个栈模拟,桶记录即可. #include <iostream> #include <cstdio> #include ...
- Ubuntu 16.04安装NASM汇编IDE-SASM
在Linux下,尤其是Ubuntu,SASM工具应该是用来开发汇编最好用的IDE,小巧且支持调试.支持的编译器有:NASM, MASM, GAS, FASM. 安装步骤: 下载: http://dow ...
- 多Tabs的横向滚动插件(支持Zepto和jQuery)
一. 效果图 二. 功能介绍 1. 支持横向移动 2. 支持点击Tab后该Tab居中 3. 拉到最左边和最右边后依然可以拉动,只是tabs的移动距离变小. 三. 使用说明 1. 在你的html中添加T ...
- SfM执行流程
整个过程根据脚本执行过程来分析. 首先我们看到RunBundler.sh,这个shell脚本. 1.定义参数 BASE_PATH="/cygdrive/e/ProjectBefore/Lea ...
- Mentor.Graphics.FloTHERM.XT.2.3+Mentor.Graphics.Flowmaster.7.9.4
Mentor.Graphics.FloTHERM.XT.2.3 Mentor.Graphics.Flowmaster.7.9.4 AVL.CRUISE.V2015.0-车辆动力学仿真分析平台 AVL. ...