Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 50499 Accepted Submission(s): 13590
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
题意:输入一个n*m的迷宫,和一个T:可以在迷宫中生存的最大时间。S为起点,D为终点。并且,每个格子只能踩一次,且只能维持一秒,然后该块地板就会塌陷。所以你必须每秒走一步,且到D点时,所用时间为T。用深搜。
感想:开始不知道 用bfs做的 WA了 才发现不是要求最小的时间 因为有些时候你需要浪费一些时间去来回走一下才
能满足条件
坑即收获:自己原来的读字符地图习惯不好 喜欢用getchar() 今天被坑了 以后不用这个了
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define maxn 10
using namespace std;
int n,m,t,flag;
int sx,sy,ex,ey;
int mp[maxn][maxn];
int vis[maxn][maxn];
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
bool judge() // 奇偶剪枝
{
int s;
s=fabs(ex-sx)+fabs(ey-sy);
if((s+t)%2==0) return true; // 奇奇 偶偶 才满足条件
return false;
}
void dfs(int nx,int ny,int cxx)
{
int i,x,y;
if(cxx>t||flag) return ;
if(nx==ex&&ny==ey&&cxx==t)
{
flag=1;
return ;
}
for(i=0;i<4;i++)
{
x=nx+dx[i];
y=ny+dy[i];
if(!mp[x][y]&&!vis[x][y])
{
vis[x][y]=1;
dfs(x,y,cxx+1);
vis[x][y]=0;
}
}
}
int main()
{
int i,j;
char c[10];
while(scanf("%d%d%d",&n,&m,&t)&&!(n==0&&m==0&&t==0))
{
getchar();
memset(mp,1,sizeof(mp));
memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
{
scanf("%s",c); // 最好这样读地图 不要用scanf("%c",c) 然后行末加一个getchar()
for(j=1;j<=m;j++)
{
if(c[j-1]=='.') mp[i][j]=0;
else if(c[j-1]=='S')
{
sx=i;
sy=j;
mp[i][j]=0;
}
else if(c[j-1]=='D')
{
ex=i;
ey=j;
mp[i][j]=0; // 这个要加上 因为终点也可走
}
}
}
if(judge())
{
flag=0;
vis[sx][sy]=1; // 这个记得标记
dfs(sx,sy,0);
if(flag) printf("YES\n");
else printf("NO\n");
}
else printf("NO\n");
}
return 0;
}