HDU---1010-Tempter of the Bone
2015-06-28 11:29
441 查看
Tempter of the Bone
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 9 Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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 themaze 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
Author
ZHANG, Zheng
Source
ZJCPC2004AC CODE:
#include <iostream> #include <cmath> #include <algorithm> #include <cstring> #include <cstdio> using namespace std; char mmap[9][9]; int dir[4][2]={{1,0},{-1,0},{0,-1},{0,1}}; int start_x,start_y,end_x,end_y; int x,y,n,poi=0; bool isplace(int xx,int yy) { if(xx<=x && yy<=y && xx>0 && yy>0) if(mmap[xx][yy]!='X') return true; return false; } void DFS(int a,int b,int temp) { if(temp==n && a==end_x && b==end_y) { poi=1; return; } if(poi)return; int t=abs(a-end_x)+abs(b-end_y)-abs(temp-n);// 优化:奇偶判断 if(t>0||t&1)return; int k,xx,yy; for(k=0; k<=3; ++k) { xx=a+dir[k][0]; yy=b+dir[k][1]; if(isplace(xx,yy)) { mmap[xx][yy]='X'; DFS(xx,yy,temp+1); mmap[xx][yy]='.'; } } } int main() { int i,j; while(scanf("%d%d%d",&x,&y,&n)!=EOF&&(x||y||n)) { if(x==0)break; for(i=1; i<=x; ++i) { for(j=1; j<=y; ++j) { cin>>mmap[i][j]; if(mmap[i][j]=='S') { start_x=i; start_y=j; } if(mmap[i][j]=='D') { end_x=i; end_y=j; } } } poi=0; mmap[start_x][start_y]='X'; DFS(start_x,start_y,0); if(poi)cout<<"YES\n"; else cout<<"NO\n"; } return 0; }
相关文章推荐
- 小贝_mysql select连接查询
- DP:树DP
- Nginx rewrite URL examples with and without redirect address
- nagios安装和配置(个人笔记)
- php+MySQL基于百度BAE的微信公众平台获取access_token
- js实现可配置参数的轮播图
- 使用Node.js配合Nginx实现高负载网络
- 将一个数组中的值按逆序重新存放。
- 有一个已排好序的数组,要求输入一个数后,按原来排序的规律将它插入数组中
- Eclipse+CDT+MinGw进行C&C++开发
- 推送消息 相关公司 手机端分享http://mob.com/
- form上传文件以及跨域异步上传
- 学习MOOC——《国际贸易学》的总结
- Spring配置web.xml的<servlet>标签
- Nginx rewrite URL examples with and without redirect address
- 求一个3*3的整型矩阵对角线元素之和
- 【Java进阶】单例模式详解
- iOS 阶段学习第九天笔记(内存管理)
- shell脚本中的引用
- 字符串-01~~~字符串-04