首页 > 学院 > 开发设计 > 正文

POJ 1010

2019-11-14 09:05:11
字体:
来源:转载
供稿:网友

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.

Note:

This question belongs to dfs family.Something we should notice is the steps mentioned in the question.And I learn a mean of cutting-edges is to calculate the steps left and x-axis and y- axis length form current point to final point,if they are both even or odd ,the path is available.If you want to testify it ,you can simply draw a map and try. and I find a good function model for dfs

void DFS( argument ){ // recursion boundary // check if the argument has reached the ans // some action // keep recursion}#include <bits/stdc++.h>using namespace std;int dir[4][2]={ {0,1} , {1,0} , {-1,0} , {0,-1} };char m[10][10];int vis[10][10];int N,M,T,tx,ty;int flag=0;bool check(int a,int b){ if(a<0||a>=N||b<0||b>=M) { return false; } else { return true; }}void dfs(int x,int y,int d){ if(check(x,y)==false) return; vis[x][y]=1; if(x==tx && y==ty && d==T) { flag=1; return;} if(((T-d)%2)!=((abs(x-tx)+abs(y-ty))%2)) return; for(int i=0;i<4;i++) { if(!vis[x+dir[i][1]][y+dir[i][0]]&&!flag) { dfs(x+dir[i][1],y+dir[i][0],d+1); vis[x+dir[i][1]][y+dir[i][0]]=0; } } }int main(){ while(cin>>N>>M>>T) { memset(m,0,sizeof(m)); memset(vis,0,sizeof(vis)); int sx,sy; if(N==0&&M==0&&T==0) { break; } else { for(int i=0;i<N;i++) { scanf("%s",m[i]); } for(int i=0;i<N;i++) { for(int j=0;j<M;j++) { char t=m[i][j]; if(t=='X'||t=='x') { vis[i][j]=1; } else { if(t=='S'||t=='s') { sx=i; sy=j; } else if(t=='D'||t=='d') { tx=i; ty=j; } } } } } flag=0; dfs(sx,sy,0); if(flag==1) { cout<<"YES"<<endl; } else { cout<<"NO"<<endl; } } return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表