HDU OJ 1010 Tempter of the Bone【深搜】
来源:互联网 发布:出售bc数据 编辑:程序博客网 时间:2024/05/21 06:25
原题连接:http://acm.hdu.edu.cn/showproblem.php?pid=1010
思路:dfs 深度优先搜索+剪枝 ;
1:因为是某个时刻正好到达door,所以要搜出所有的能到达door的情况,比较时间是否相等,故用深搜!!
2:只有一般的深搜搜索提交会超时,所以需要剪枝。。
剪枝如下:
1.如果当前搜索的深度(时间)大于时间t,中止搜索。
2.如果已经搜索到结果,中止搜索。
3.如果当前所剩余的时间小于从当前位置到终点的理论最短时间,必定不能到达,中止搜索。
4.传说中的奇偶性剪枝,详细介绍如下:
/* 0 1 0 1 0 1 1 0 1 0 1 0 0 1 0 1 0 1 1 0 1 0 1 0*/
它的规律是:0和1相间排列。仔细观察不难发现,当行号和列号同为奇数,或者同为偶数时,写0;否则,写1。
而且,从0的格子走1步,不管哪个方向,都会走到1的格子上。
于是我们得出这样的结论:
0->1 或 1->0 必定走奇数步,
0->0 或 1->1 必定走偶数步。
所以当我们遇到不满足上述两个结论的,可以推断它不能到达,所以中止搜索。
还有一种特殊情况,就是当地图上能走的点的个数(不包括起点)小于时间t的时候,显然不能到达,这时可以不用搜索,直接输出NO;
ac代码::还有就是注意地图的存储!!!。。
#include<stdio.h>#include<iostream>#include<string.h>using namespace std;int sx[]={0,0,1,-1},zy[]={1,-1,0,0};int ac[10][10],n,m,t,end_x,end_y,ok;char map[10][10];int fun(int x,int y){ if(x>y) return x-y; else return y-x;}void dfs(int i,int j ,int step ){ int k,a,b; if(ok==1) return; if(step==t) { if(map[i][j]=='D') ok=1; return; } k=fun(i,end_x)+fun(j,end_y); if(k+step>t) return; b=t-step-k; if(k%2!=(t-step)%2) return; for(a=0;a<4;a++) { int x= i+sx[a]; int y= j+zy[a]; if(x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='X'&&ac[x][y]==0) { ac[x][y]=1; dfs(x,y,step+1); ac[x][y]=0; } }}int main(){ int a,b,i,j; while(scanf("%d%d%d",&n,&m,&t)) { ok=0; if(n+m+t==0) break;getchar(); for(a=0;a<n;a++) { for(b=0;b<m;b++) { cin>>map[a][b]; if(map[a][b]=='S') { i=a; j=b; } else if(map[a][b]=='D') { end_x=a; end_y=b; } ac[a][b]=0; } getchar(); } ac[i][j]=1; dfs(i,j,0); if(ok) printf("YES\n"); else printf("NO\n"); }}
- HDU-OJ-1010 Tempter of the Bone
- HDU OJ 1010 Tempter of the Bone【深搜】
- hdu 1010 Tempter of the Bone(深搜)
- HDU 1010 Tempter of the Bone(深搜)
- Tempter of the Bone(HDU 深搜)
- HDU 1010 Tempter of the Bone
- 迷宫 hdu 1010 Tempter of the Bone
- HDU 1010-Tempter of the Bone
- hdu 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone
- HDU 1010 Tempter of the Bone
- hdu 1010 Tempter of the Bone
- HDU-1010-Tempter of the Bone
- hdu 1010 Tempter of the Bone
- HDU 1010-Tempter of the Bone
- 80端口(该端口是Tomcat的监听端口)已经被其他程序占用
- 6 个效果震撼的 jQuery 图片播放器
- gdi画图基本步骤
- 自定义uitabview section
- 非专业人士观点(1)性价比无法比
- HDU OJ 1010 Tempter of the Bone【深搜】
- Linux 文件目录操作指令
- UIPageControl 实现自定义样式
- CM3-读书笔记《Cortex-M3权威指南Cn》-2
- 一篇程序员应该看下的文章
- apt-get 命令详解(中文),以及实例
- C#学习之路--DLL参数转换
- iphone调用系统电话、浏览器、地图、邮件等
- usb摄像头在s3c6410平台上的测试