hdu 1010 Tempter of the Bone
来源:互联网 发布:php视频网站模板 编辑:程序博客网 时间:2024/05/19 08:45
题目大意
给出一个N*M的迷宫和一个规定的时间T,离开的要求是主角必须在T时刻准时到出口.求是否主角能够离开迷宫.
解题方法
对于迷宫中的每一个点,只能够走一次,并且不是求最短时间.显然这时候使用BFS解法是不合适的.当然,如果你觉得合适的话,可以留言探讨.所以,这题的解法应当是DFS.
解题步骤
一开始我是直接写DFS然后提交发现超时.然后开始想剪枝.这里我用到了2个剪枝.
第一个剪枝是奇偶性剪枝.假设从点(x,y)到(ex,ey)需要的最小步数为t,t = abs(x-ex) + abs(y-ey); 那么如果当前时间剩余的比最小步数多的话,则需要多走偶数步,如图所示.
第二个剪枝是判断剩下所有可走的格子是否小于可用时间.如果剩下可走的格子数比时间要少,则说明怎么样都无法再规定时间到达出口.注意,可走的格子数等于M*N-墙的数量 - 1.,因为起点已经走过了.
这两个剪枝都不能少,如果少掉任何一个,还是会超时
解题代码
#include <iostream>#include <string.h>#include <stdio.h>#include <queue>#include <math.h>using namespace std;int N,M,T;int sx,sy,ex,ey,cnt;char map[10][10];bool visit[10][10],flag;int dir[4][2] = {0,1,0,-1,1,0,-1,0};int abs(int a);bool canNotWalk(int x,int y){ if(x < 0 || x > N - 1 || y < 0 || y > M - 1 || map[x][y] == 'X') return true; return false;}void dfs(int x,int y,int t){ if(t < 0 || flag) return ; if(abs(x-ex) + abs(y-ey) > t) return ; if( (t - abs(x-ex) - abs(y-ey)) % 2) return ; if(t == 0 & x == ex && y == ey) { flag = true; return ; }// printf("%d %d %d\n",x,y,t); for(int i = 0; i < 4; i++) { if(visit[x][y] || canNotWalk(x,y) ) continue; visit[x][y] = true; dfs(x+dir[i][0],y+dir[i][1],t-1); visit[x][y] = false; }}int main(){// freopen("1.txt","r",stdin); while(~scanf("%d%d%d",&N,&M,&T)&&(N||M||T)) { cnt = 0; for(int i = 0; i < N; i++) scanf("%s",map+i); for(int i = 0; i < N; i++) for(int j = 0; j < M; j++) if(map[i][j] == 'S') sx = i,sy = j; else if(map[i][j] == 'D') ex = i,ey = j; else if(map[i][j] == 'X') cnt ++; if(N * M - cnt - 1 < T){ puts("NO"); continue; } flag = false; visit[sx][sy] = true; memset(visit,false,sizeof(visit)); dfs(sx,sy,T); if(flag) puts("YES"); else puts("NO"); } return 0;}int abs(int a){ return a > 0 ? a : -a;}
0 0
- 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
- HDU 1010-Tempter of the Bone
- HDU 1010 Tempter of the Bone (DFS)
- hdu 1010 Tempter of the Bone
- HDU 1010 - Tempter of the Bone
- hdu 1010 Tempter of the Bone
- 我们为什么还要sim卡
- 韩红4月24日来菏泽来了,看的人真多人山人海
- libsvm简单入门
- 利用Java自带的Base64加密,解密
- cmd 命令行方式执行 matlab 脚本
- hdu 1010 Tempter of the Bone
- Android蓝牙4.0的使用
- 处理数字加小数点的字符串
- Java中的随机数生成器:Random,ThreadLocalRandom,SecureRandom
- onPrepareOptionsMenu 和onCreateOptionsMenu 的区别
- -Dmaven.multiModuleProjectDirectory system propery is not set.
- Linux下远程连接Jupyter+pyspark部署教程
- UI测试导入Espresso时的冲突
- Material Design: NavigationView FlaotingActionBar SnackBar的使用,navigationview