Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 155578 Accepted Submission(s): 41455
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
Recommend
JGShining
题目简述:
有条狗困入迷宫,迷宫每块地板踩一次就会下沉,整个迷宫只有一个门且仅在T时刻开启,问小狗能不能出去。
题目分析:
先奇偶剪枝,然后dfs,刚开始用bfs,后来才发现求得不是最短路径。。。
代码实现:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<stack>
#include<cstdlib>
#include<cmath>
#include<queue>
//#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
#define pf printf
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define ms(i,j) memset(i,j,sizeof(i))
struct s
{
int x,y,s;
} tmp,now,bgn,END;
bool mark[6][6];
char a[6][6];
int n,m,time,tt,d[4][2]={-1,0,1,0,0,-1,0,1};
bool dfs(int x,int y,int t)
{
int tx,ty;
if(x==END.x&&y==END.y&&t==END.s)
{
return 1;
}
if(t>=END.s)
{
return 0;
}
for(int i=0;i<4;i++)
{
tx=x+d[i][0];
ty=y+d[i][1];
mark[x][y]=1;
if(tx>=0&&tx<n&&ty>=0&&ty<m&&a[tx][ty]!='X'&&!mark[tx][ty]&&dfs(tx,ty,t+1))
return 1;
mark[x][y]=0;
}
return 0;
}
int main()
{
while(sfff(n,m,time)&&n)
{
ms(mark,0);
ms(a,0);
for(int i=0; i<n; i++)
{
scanf("%s",a[i]);
for(int j=0; j<m; j++)
{
if(a[i][j]=='S')
bgn= {i,j,0};
if(a[i][j]=='D')
END= {i,j,time};
}
}
tt=abs(bgn.x-END.x)+abs(bgn.y-END.y);
if(time<tt||(time-tt)%2)
{
pf("NO\n");
continue;
}
if(dfs(bgn.x,bgn.y,0))
pf("YES\n");
else
pf("NO\n");
}
}