hdu 1010 Tempter of the Bone dfs+剪枝
来源:互联网 发布:全球网络支付平台 编辑:程序博客网 时间:2024/05/21 09:44
题意:给定一个n*m的迷宫,'S'是起点,'D'是门,'X'是墙,'.'是路,每次走过路之后,路会塌陷不能再走,给定一个时间t,门只会在时间正好为t的时候才开。问是否可以从门中逃出去。
题解:由于给定了特定的时间,所以需要dfs,直到时间,位置都符合才能出门。由于耗时很长,所以需要剪枝。1)当时间到t却还没到门口,那之后就不用再走了;2)从起点到门的最短距离为d,那么中间无论怎么乱走都是d+2*x的步数(想想就能知道),即d与t同奇同偶,一开始可以排除大部分情况;3)路的个数比t小,也可以排除
代码:
#include <stdio.h>#include <stdlib.h>#include <math.h>#include <string.h>#include <iostream>#include <algorithm>#include <map>#include <set>#include <queue>using namespace std;const int maxn=10;char e[maxn][maxn];int vis[maxn][maxn],flag;int n,m;int dir[4][2]={{1,0},{-1,0},{0,-1},{0,1}};void dfs(int num,int x,int y,int x2,int y2,int t){ if(flag)return; int xx,yy,i; for(i=0;i<4;i++) { xx=x+dir[i][0]; yy=y+dir[i][1]; //printf("%d %d\n",xx,yy); if(xx<0||yy<0||xx>=n||yy>=m||e[xx][yy]=='X')continue; //printf("%d %d->%d %d\n",x,y,xx,yy); if(num+1==t) { if(xx==x2&&yy==y2){flag=1;return;} continue; } if(xx==x2&&yy==y2)continue; e[xx][yy]='X'; dfs(num+1,xx,yy,x2,y2,t); if(flag)return; e[xx][yy]='.'; }}int main(){ int t; while(scanf("%d%d%d",&n,&m,&t)!=EOF) { if(n==0)break; int i,j,k,x1,y1,x2,y2; for(i=0;i<n;i++) scanf("%s",e[i]); memset(vis,0,sizeof(vis)); int num=0; for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(e[i][j]=='S'){x1=i;y1=j;} if(e[i][j]=='D') { num++; x2=i;y2=j; } if(e[i][j]=='.')num++; } } if((abs(x2-x1)+abs(y2-y1)+t)%2){printf("NO\n");continue;}//剪枝 if(num<t) { printf("NO\n"); continue; } flag=0; //printf("%d %d %d %d\n",x1,y1,x2,y2); e[x1][y1]='X';//不加会出现下面给的第二组数据错误 dfs(0,x1,y1,x2,y2,t); if(flag)printf("YES\n"); else printf("NO\n"); } return 0;}/*2 2 1SD..3 3 3S..DX....*/
0 0
- hdu 1010 Tempter of the Bone(DFS+剪枝优化)
- hdu 1010 Tempter of the Bone(dfs 剪枝)
- HDU 1010 Tempter of the Bone - (DFS) 奇偶剪枝
- HDU 1010 Tempter of the Bone(DFS+奇偶剪枝)
- hdu 1010 Tempter of the Bone(dfs+奇偶剪枝)
- hdu 1010 Tempter of the Bone(dfs+奇偶剪枝)
- HDU--1010 -- Tempter of the Bone [DFS+经典奇偶剪枝]
- HDU 1010Tempter of the Bone(奇偶剪枝回溯dfs)
- hdu 1010 Tempter of the Bone (dfs+剪枝)
- HDU 1010 Tempter of the Bone (ZOJ 2110) DFS+剪枝
- Hdu 1010 Tempter of the Bone (DFS 经典奇偶剪枝)
- HDU-#1010 Tempter of the Bone(DFS+剪枝)
- hdu 1010 Tempter of the Bone (dfs+奇偶剪枝)
- HDU 1010 Tempter of the Bone( DFS + 奇偶剪枝 )
- HDU 1010 Tempter of the Bone(DFS+剪枝)
- HDU 1010 Tempter of the Bone(DFS回溯+剪枝)
- hdu 1010 Tempter of the Bone(dfs + 奇偶剪枝)
- hdu 1010 Tempter of the Bone dfs+剪枝
- 揭秘Microsoft Windows LDM
- UIApplication 详细解释-IOS
- 一个简单的日历
- 【转】线性筛素数,线性筛欧拉函数,求前N个数的约数个数
- iOS- 使用正则表达式NSRegularExpression 来验证textfiled输入的内容
- hdu 1010 Tempter of the Bone dfs+剪枝
- (求助)安卓指纹解锁功能
- UIPickerView控件简单使用
- 寒城攻略:Listo 教你 25 天学会 Swift 语言 - 13 Methods
- ubuntu 常用命令
- 中国城市人口排名(最新版)
- Swift编程语言学习6—— 闭包
- 解决网页乱码的终极方法。
- 自学知识