[hdu]Tempter of the Bone
来源:互联网 发布:unity3d源码下载 编辑:程序博客网 时间:2024/05/17 23:13
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 85139 Accepted Submission(s): 23194
Total Submission(s): 85139 Accepted Submission(s): 23194
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.
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.
'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 5S.X...X...XD....3 4 5S.X...X....D0 0 0
Sample Output
NOYES
题意:小狗走迷宫。S是开始D是结束,X是墙,问能否在T时刻恰好到达重点。注意是恰好。
题解:因为问的是恰好,所以应该用的是深度优先搜索。注意奇偶剪枝,回溯法也是个重点。
#include "stdio.h"#include "cmath"#include "iostream"using namespace std;int N,M,T;char a[10][10];int vis[10][10];int ax,ay,bx,by,flag;int dir[4][2]={{0,-1},{0,1},{-1,0},{1,0}};//上下左右 void dfs(int x,int y,int t){int n,m,i;if(x==bx&&y==by&&t==T){flag=1;return ;}if(t>=T)return ;for(i=0;i<4;i++)//遍历上下左右 {if(x+dir[i][0]>=1&&x+dir[i][0]<=N&&y+dir[i][1]>=1&&y+dir[i][1]<=M)//判断是否出格 {n=x+dir[i][0];m=y+dir[i][1]; if(a[n][m]!='X'&&vis[n][m]!=1){vis[n][m]=1;dfs(n,m,t+1);//开始深搜 if(flag>0)return ;vis[n][m]=0;//回溯还原标记 }}}}int main(){int t;while(scanf("%d %d %d",&N,&M,&T)==3&&(N+M+T)){for(int i=1;i<=N;i++){getchar();//谜之回车 。。。 for(int j=1;j<=M;j++){cin>>a[i][j];if(a[i][j]=='S'){ax=i;ay=j;}if(a[i][j]=='D'){bx=i;by=j;}}}flag=0;t=0;memset(vis,0,sizeof(vis));if(abs(ax-bx)+abs(ay-by)>T||(ax+bx+ay+by)%2!=T%2)//普通剪枝和奇偶剪枝。。感觉有一些弱 {cout<<"NO"<<endl;continue;}vis[ax][ay]=1;dfs(ax,ay,t);if(flag==0)cout<<"NO"<<endl;else cout<<"YES"<<endl;}return 0;}
0 0
- [hdu]Tempter of the Bone
- HDU Tempter of the Bone
- Tempter of the Bone HDU
- Tempter of the Bone HDU
- Tempter of the Bone HDU
- Tempter of the Bone HDU
- HDU 1010 Tempter of the Bone
- HDU 2128 Tempter of the Bone II
- 迷宫 hdu 1010 Tempter of the Bone
- HDU 1786 Tempter of the Bone again
- 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
- 三层架构--如何判断一项功能到底属于哪一程
- UI进阶 —— 收起键盘方法
- Python入门-函数的使用到程序的发布安装
- Codeforces Round #306 (Div. 2) A
- Android Studio快捷键
- [hdu]Tempter of the Bone
- HDU 不容易系列之(3)—— LELE的RPG难题
- OpenStack行业实践和发展趋势
- Android的TabHost组件-android的学习之旅(四十)
- Odroid U3 烧写镜像文件
- CCNA学习指南 IP路由
- VNR共享辞书中翻译的转码和代理
- ActionScript 3.0 学习(二) Flash Builder找不到所需版本的Adobe Flash Player
- java内置数据结构--Map接口与特性