hdu 1010 Tempter of the Bone
来源:互联网 发布:python双击闪退 编辑:程序博客网 时间:2024/06/03 14:35
The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.
The input is terminated with three 0's. This test case is not to be processed.
4 4 5S.X...X...XD....3 4 5S.X...X....D0 0 0
NOYES经典剪枝问题,加深理解定义:假设起点为(sx,sy),终点为(ex,ey),给定t步恰好走到终点,若 t-[abs(ex-sx)+abs(ey-sy)] 结果为非偶数(奇数),则无法在t步恰好到达剪枝方法:奇偶剪枝
把map看作
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从 0->1 需要奇数步
从 0->0 需要偶数步
那么设所在位置 (x,y) 与 目标位置 (dx,dy)如果abs(x-y)+abs(dx-dy)为偶数,则说明 abs(x-y) 和 abs(dx-dy)的奇偶性相同,需要走偶数步
如果abs(x-y)+abs(dx-dy)为奇数,那么说明 abs(x-y) 和 abs(dx-dy)的奇偶性不同,需要走奇数步
理解为 abs(si-sj)+abs(di-dj) 的奇偶性就确定了所需要的步数的奇偶性!!
而 (ti-setp)表示剩下还需要走的步数,由于题目要求要在 ti时 恰好到达,那么 (ti-step) 与 abs(x-y)+abs(dx-dy) 的奇偶性必须相同
因此 temp=ti-step-abs(dx-x)-abs(dy-y) 必然为偶数!
最后一点:整个图的可以移动步数应该大于指定的时间
#include<iostream>#include<cstring>#include<cmath>using namespace std;char map[10][10];bool ok;int n,m,t,sx,sy,ex,ey,vis[10][10];int to[4][2]={0,1,0,-1,1,0,-1,0};void dfs(int x,int y,int st){ int i,dx,dy; if(st==t) { if(map[x][y]=='D') ok=1; return ; } if(ok) return ; int p=abs(ex-x)+abs(ey-y)-abs(st-t);//奇偶剪枝 if(p>0||p&1) return ; for(i=0;i<4;i++) { dx=x+to[i][0],dy=y+to[i][1]; if(dx>=1&&dx<=n&&dy>=1&&dy<=m&&map[dx][dy]!='X'&&!vis[dx][dy]) { vis[dx][dy]=1; dfs(dx,dy,st+1); vis[dx][dy]=0; } } return ;}int main(){ int i,j; while(cin>>n>>m>>t,n||m||t) { ok=0; memset(vis,0,sizeof vis); for(i=1;i<=n;i++) cin>>map[i]+1; for(i=1;i<=n;i++) { for(j=1;j<=m;j++) { if(map[i][j]=='S') { sx=i,sy=j; } else if(map[i][j]=='D') { ex=i,ey=j; } } } vis[sx][sy]=1; dfs(sx,sy,0); if(ok) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0;}
一个更全面的剪枝#include <stdio.h> #include <string.h> #include <math.h> int n,m,t; char map[10][10]; int flag; int di,dj,wall; int to[4][2] = {{0,-1},{0,1},{-1,0},{1,0}}; void dfs(int si,int sj,int cnt)//深搜 { int i,tem; if(si>n || sj>m || si<=0 || sj <= 0)//出界 return ; if(cnt == t && si == di && sj == dj)//到达终点 flag = 1; if(flag) return ; int s1 = si-di; int s2 = sj-dj; if(s1<0) s1=-s1; if(s2<0) s2=-s2; tem = t-cnt - s1 - s2; if(tem<0 || tem&1)//看剩下的时间能能否到达终点,tem&1则是判断其是否偶数,根据LCY的奇偶性剪枝可得tem必须是偶数,是奇数则不行 return; for(i = 0; i<4; i++) { if(map[si+to[i][0]][sj+to[i][1]]!='X') { map[si+to[i][0]][sj+to[i][1]]='X';//走过的地方变为墙 dfs(si+to[i][0],sj+to[i][1],cnt+1); map[si+to[i][0]][sj+to[i][1]]='.';//迷宫还原,以便下次搜 } } return ; } int main() { int i,j,si,sj; while(~scanf("%d%d%d%*c",&n,&m,&t)) { if(!n && !m && !t) break; wall = 0; for(i = 1; i<=n; i++) { for(j = 1; j<=m; j++) { scanf("%c",&map[i][j]); if(map[i][j] == 'S') { si = i; sj = j; } else if(map[i][j] == 'D') { di = i; dj = j; } else if(map[i][j] == 'X') wall++; } getchar(); } if(n*m-wall<=t)//t是代表要走的步数,步数加墙数必须小于总格子数的,因为所有格子中还包括了S和D,这是剪枝 { printf("NO\n"); continue; } flag = 0; map[si][sj] = 'X';//出发点是不可能再走的了,变为墙 dfs(si,sj,0); if(flag) printf("YES\n"); else printf("NO\n"); } return 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
- CodeFroces 445B DZY Loves Chemistry
- Sort List LeetCode
- iOS音频播放 (一):概述 转
- poj 2540 && uva 10084 Hotter Colder(半平面交)
- 取数游戏_签到积分
- hdu 1010 Tempter of the Bone
- poj 1015 Jury Compromise dp
- 黑马程序员_集合框架工具类
- sizeof小结
- 解决"Windows系统自带共享代理软件导致的断网问题"
- C#基础 之 params、ref、out
- leetcode Search in Rotated Sorted Array
- 【Orz5】 2014.08.09集训总结
- jdbcutil