Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 83382 Accepted Submission(s): 22716
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.
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 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
Sample Output
NO YES
以前没过的题,今天自己KO了。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
int n,m,time;
int startx,starty,endx,endy;//起点坐标 终点坐标
char map[8][8];
int visit[8][8];//标记
int exist;//判断是否可以存活
void getmap()
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='S')
{
startx=i;
starty=j;
}
if(map[i][j]=='D')
{
endx=i;
endy=j;
}
}
getchar();
}
}
int judge(int x,int y)//判断能否走当前位置
{
if(!visit[x][y]&&x>=0&&x<n&&y>=0&&y<m&&map[x][y]!='X')
return 1;
else
return 0;
}
void dfs(int use,int x,int y)//use记录当前搜索所用时间
{
int k;
int next_x,next_y;//下一坐标
int move[4][2]={0,1,0,-1,1,0,-1,0};
if(exist)//已找到可行路径
return ;
if(x==endx&&y==endy&&use==time)//找到路径
{
exist=1;
return ;
}
if((time-abs(x-endx)-abs(y-endy)-use)&1)//剪枝
return ;
for(k=0;k<4;k++)
{
next_x=x+move[k][0];
next_y=y+move[k][1];
if(judge(next_x,next_y))
{
visit[next_x][next_y]=1;
dfs(use+1,next_x,next_y);
visit[next_x][next_y]=0;
}
}
}
int main()
{
while(scanf("%d%d%d",&n,&m,&time)&&(n!=0||m!=0||time!=0))
{
getchar();
getmap();
memset(visit,0,sizeof(visit));//初始化
visit[startx][starty]=1;
exist=0;
dfs(0,startx,starty);//搜索开始
if(exist)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}