HDU Tempter of the Bone(DFS+奇偶剪枝)
2016-01-12 21:38
281 查看
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 95890 Accepted Submission(s): 25982
Problem Description
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately
to get out of this maze.
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.
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively.
The next N lines give the maze layout, with each line containing M characters. A character is one of the following:
'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.
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
Sample Output
NO YES
1.奇偶剪枝:http://baike.baidu.com/view/7789287.htm 百度百科,假设起点是sx,sy终点是ex,ey那么abs(ex-sx)+abs(ey-ey)为起点到终点的最短步数。起点到终点的步数要么是最短步数(最短步数+0),要么是最短步数+一个偶数(偏移路径)。
2.代码:
#include<cstdio> #include<cstring> using namespace std; char mat[10][10]; bool vis[10][10]; int n,m,t; int xs,ys,xd,yd; bool flag; int dir[4][2]={1,0,-1,0,0,-1,0,1};//up,lower,left,right int ABS(int a) { if(a<0) return -a; else return a; } void dfs(int x,int y,int time) { if(x<0||x>=n||y<0||y>=m) return; if(mat[x][y]=='X') return; if(vis[x][y]==1) return; vis[x][y]=1; if(x==xd&&y==yd&&time==t) { flag=true; return; } if(time==t&&(x!=xd||y!=yd)) return; if(time>t) return; for(int i=0;i<4;i++) { int xt=x+dir[i][0]; int yt=y+dir[i][1]; if(xt<0||xt>=n||yt<0||yt>=m||mat[xt][yt]=='X'||vis[xt][yt]==1) continue; dfs(xt,yt,++time); vis[xt][yt]=0; time--; if(flag==1) return; } } int main() { while(scanf("%d%d%d",&n,&m,&t)&&(n||m||t)) { for(int i=0;i<n;i++) { scanf("%s",mat[i]); for(int j=0;j<m;j++) { if(mat[i][j]=='S') { xs=i; ys=j; } if(mat[i][j]=='D') { xd=i; yd=j; } } } int ms=ABS(xd-xs)+ABS(yd-ys); if(t<ms) { printf("NO\n"); continue; } if((t-ms)%2) { printf("NO\n"); continue; } flag=false; memset(vis,0,sizeof(vis)); dfs(xs,ys,0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- iOS_时间相关
- SQLite数据库常用语句及MAC上的SQLite可视化工具MeasSQLlite使用
- CodeForces--609C --Load Balancing(水题)
- PopupWindow
- linux链接--硬链接,软链接
- 机器人操作系统ROS总结
- Java设计模式——单例设计模式
- 快速幂求余
- 附2 volatile
- 【原】模式之-适配器Adapter模式
- html之file标签 --- 图片上传前预览 -- FileReader
- hdoj Watch The Movie 3496 (二维01背包)好题
- Android permission 动态申请、授权
- 3、Java常用的运算符
- Linux进程间通信之管道
- 解决离线Could not parse configuration:hibernate.cfg.xml错误
- Android中将AS的项目导入到eclipse中,stye中常常报错
- Spring对RMI的支持
- C Primer Plus(第五版)9
- casperJs的安装