Tempter of the Bone DFS+奇偶剪枝
2014-02-28 21:12
141 查看
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
Sample Output
Author
ZHANG, Zheng
Source
ZJCPC2004
Recommend
JGShining | We have carefully selected several similar problems for you: 1016 1241 1242 1072 1312
AC代码:
WA代码:
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
Recommend
JGShining | We have carefully selected several similar problems for you: 1016 1241 1242 1072 1312
AC代码:
#include <stdio.h> int dx[4]={-1,0,0,1}; int dy[4]={0,-1,1,0}; char map[10][10]; int N,M,T; int sx,sy,ex,ey; int abs(int x){ return x>=0?x:-x; } int DFS(int x,int y,int step){ int i; int x1,y1; int tmp=T-step-abs(ex-x)-abs(ey-y); if(tmp<0||tmp%2){ return 0; } for(i=0;i<4;++i){ x1=x+dx[i]; y1=y+dy[i]; if(x1>=1&&x1<=N&&y1>=1&&y1<=M){ if(step+1==T&&map[x1][y1]=='D'){ return 1; } if(map[x1][y1]=='.'){ map[x1][y1]='X'; if(DFS(x1,y1,step+1)) return 1; map[x1][y1]='.'; } } } return 0; } int main(){ int i,j; while(scanf("%d%d%d",&N,&M,&T)){ if(!N&&!M&&!T) return 0; for(i=1;i<=N;++i) for(j=1;j<=M;++j){ scanf(" %c",&map[i][j]); if(map[i][j]=='S'){ sx=i; sy=j; } else if(map[i][j]=='D'){ ex=i; ey=j; } } map[sx][sy]='X'; if(DFS(sx,sy,0)) printf("YES\n"); else printf("NO\n"); } return 0; }
WA代码:
#include <stdio.h> int dx[4]={-1,0,0,1}; int dy[4]={0,-1,1,0}; char map[10][10]; int N,M,T; int sx,sy,ex,ey; int flag; int abs(int x){ return x>=0?x:-x; } void DFS(int x,int y,int step){ int i; int x1,y1; if(!flag){ int tmp=T-step-abs(ex-x)-abs(ey-y); if(tmp<0||tmp%2){ //中间会出现tmp%2!=0的情况。 printf("NO\n"); flag=1; return; } for(i=0;i<4&&!flag;++i){ x1=x+dx[i]; y1=y+dy[i]; printf("%d %d\n",x1,y1); if(x1>=1&&x1<=N&&y1>=1&&y1<=M){ if(step+1==T&&map[x1][y1]=='D'){ printf("YES\n"); flag=1; return; } if(map[x1][y1]=='.'){ map[x1][y1]='X'; DFS(x1,y1,step+1); map[x1][y1]='.'; } } } } return; } int main(){ int i,j; while(scanf("%d%d%d",&N,&M,&T)){ if(!N&&!M&&!T) return 0; flag=0; for(i=1;i<=N;++i) for(j=1;j<=M;++j){ scanf(" %c",&map[i][j]); if(map[i][j]=='S'){ sx=i; sy=j; } else if(map[i][j]=='D'){ ex=i; ey=j; } } map[sx][sy]='X'; DFS(sx,sy,0); if(!flag) printf("NO\n"); } return 0; }
相关文章推荐
- WIN8局域网速度超慢
- Binder框架在Framework层的C++中的使用
- 经典编程书籍(C++, 网络, Windows, Linux)
- Arm Cortex-M0新唐m052外部中断
- Java Web Service eclipse安装 环境 使用命令
- 借助Canvas上传图片
- struts2随笔(一)Action、struts.xml、Interceptor细节
- poj 1940 Wine Trading in Gergovia_贪心
- 直接插入排序
- SOLID rule in JAVA design.
- poj 1940 Wine Trading in Gergovia_贪心
- 合并排序--分治法思想
- 蓝桥杯 危险系数 (求两点间割点个数)
- 2013年下半年总结
- 当在写android的xml文件中出现Unexpected text found in layout file: """的解决办法
- hdu,2037,今年暑假不AC
- C#中Graphics的画图代码【转】
- UML图(三)类图
- 设计一个类,只能实现1个实例或3个实例
- 数据库操作类util