首页 > 代码库 > Tempter of the Bone
Tempter of the Bone
Tempter of the Bone
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64uDescription
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
水题,,只不过 加了一个奇偶性的剪枝
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<string>#include<cstdlib>#include<algorithm>#include<queue>#include<vector>#include<set>using namespace std;int n,m,t,sx,sy,ex,ey;int dic[4][2]={{0,1},{0,-1},{-1,0},{1,0}};char s[10][10];bool flag,vis[10][10];bool check(int x,int y){ if(x<0||x>=n||y<0||y>=m||vis[x][y]||s[x][y]==‘X‘) return false; return true;}void dfs(int x,int y,int step){ if(flag) return ; if(step+abs(x-ex)+abs(y-ey)>t) return ; if((t-step-abs(x-ex)-abs(y-ey))%2!=0||t-step<0) return ; if(x==ex&&y==ey&&step==t) { flag=true; return ; } for(int i=0;i<4;i++) { int xx,yy; xx=x+dic[i][0],yy=y+dic[i][1]; if(check(xx,yy)) { vis[xx][yy]=1; dfs(xx,yy,step+1); if(flag) return ; vis[xx][yy]=0; } } return ;}int main(){ while(scanf("%d%d%d",&n,&m,&t)!=EOF) { memset(vis,0,sizeof(vis)); flag=false; if(n==0&&m==0&&t==0) break; for(int i=0;i<n;i++) scanf("%s",s[i]); for(int i=0;i<n;i++) for(int j=0;j<m;j++) { if(s[i][j]==‘S‘) sx=i,sy=j; else if(s[i][j]==‘D‘) ex=i,ey=j; } vis[sx][sy]=1; dfs(sx,sy,0); if(flag) printf("YES\n"); else printf("NO\n"); } return 0;}
Tempter of the Bone
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。