HDU 1010 Tempter of the Bone(bfs)
2016-02-01 19:42
429 查看
Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 97219 Accepted Submission(s): 26383 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 Author ZHANG, Zheng Source ZJCPC2004 |
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; int n,m,t; bool v[8][8]; char a[8][8]; int sx,sy,ex,ey; int f; int d[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; void dfs(int x,int y,int cnt) { if(cnt==t&&x==ex&&y==ey){ f=1; return ; } if(f) return ; for(int i=0;i<4;i++){ int xx=x+d[i][0]; int yy=y+d[i][1]; if(xx<0||xx>=n||yy<0||yy>=m||a[xx][yy]=='X'||v[xx][yy]) continue; v[xx][yy]=1; dfs(xx,yy,cnt+1); v[xx][yy]=0; } } int main() { while(cin>>n>>m>>t&&n&&m&&t){ memset(v,0,sizeof(v)); for(int i=0;i<n;i++){ for (int j=0;j<m;j++){ cin>>a[i][j]; if(a[i][j]=='S'){ sx=i;sy=j; } if(a[i][j]=='D'){ ex=i;ey=j; } } } int ret=abs(ex-sx)+abs(ey-sy)-t;//剪枝 if(ret>0||ret%2){ cout<<"NO"<<endl; continue; } f=0; v[sx][sy]=1; dfs(sx,sy,0); if(f) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
相关文章推荐
- 关于C语言里getchar和scanf的思考
- tnsping 解析
- app兼容性测试
- PAT 1001. A+B Format (20)
- HDU 2717 Catch That Cow(bfs)
- postgresql关于auto vacuum
- 四个害人小程序解法
- Linux基本的快捷键
- javascript实现贪吃蛇
- 互联网产品上线前,做些什么——产品、开发、测试的视角
- xshell下bpython使用退格键的问题
- K度图的着色,uva1613
- MFC 实现浏览按钮选择文件
- 硬盘分区表知识——详解硬盘MBR
- HDU 1061 Rightmost Digit(找规律)
- vim命令常用
- 3236: [Ahoi2013]作业
- Linux线程 -- 互斥锁实践
- Qt线程QThread简析(8个线程等级,在UI线程里可调用thread->wait()等待线程结束,exit()可直接退出线程,setStackSize设置线程堆栈,首次见到Qt::HANDLE,QThreadData和QThreadPrivate)
- 缓存穿透与缓存雪崩