Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 129718 Accepted Submission(s): 35018
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
#include <stdio.h>
#include <stdlib.h>
int di,dj,n,m,t;
int map[10][10];
int flag=0;
void dfs(int si,int sj,int cnt)
{
if(si<=0||sj<=0||si>n||sj>m)return;
if(si==di&&sj==dj&&cnt==t)flag=1;
if(flag)return;
if((t-cnt)%2==(abs(si-di)+abs(sj-dj))%2)
if(map[si+1][sj]!='X')
{
map[si+1][sj]='X';
dfs(si+1,sj,cnt+1);
map[si+1][sj]='.';
}
if(map[si-1][sj]!='X')
{
map[si-1][sj]='X';
dfs(si-1,sj,cnt+1);
map[si-1][sj]='.';
}
if(map[si][sj+1]!='X')
{
map[si][sj+1]='X';
dfs(si,sj+1,cnt+1);
map[si][sj+1]='.';
}
if(map[si][sj-1]!='X')
{
map[si][sj-1]='X';
dfs(si,sj-1,cnt+1);
map[si][sj-1]='.';
}
return;
}
int main(int argc, char *argv[])
{
while(scanf("%d %d %d",&n,&m,&t),n+m+t)
{
getchar();
int i,j,si,sj;
int wall=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='S')
{
si=i;
sj=j;
}
if(map[i][j]=='D')
{
di=i;
dj=j;
}
if(map[i][j]=='X')
{
wall++;
}
}
getchar();
}
if(n*m-wall<=t)
{
printf("NO\n");
continue;
}
map[si][sj]='X';
flag=0;
dfs(si,sj,0);
if(flag==1)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}DFS搜索入门,本来是直接从当前的点直接四方搜索,结果错了,原来判定条件map[si][sj]!=‘X’出错,应该是从下一个非X得点开始搜索所以过了2.0版本
#include <stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
char map[100][100];
int n,m;
int num;
bool judge;
int di,dj;
int step[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
void dfs(int x,int y,int cnt)
{
if(x<=0||y<=0||x>n||y>m)
return ;
if(judge)return ;
if(x==di&&y==dj&&cnt==num)
{
judge=1;
return ;
}
int i;
if((num-cnt)%2==(abs(x-di)+abs(y-dj))%2)
{
for(i=0;i<4;i++)
{
if(map[x+step[i][0]][y+step[i][1]]!='X')
{
map[x+step[i][0]][y+step[i][1]]='X';
dfs(x+step[i][0],y+step[i][1],cnt+1);
map[x+step[i][0]][y+step[i][1]]='.';
}
}
}
return ;
}
int main(int argc, char *argv[])
{
while(scanf("%d %d %d",&n,&m,&num),n+m+num)
{
int i,j,si,sj;
int max=0;
getchar();
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='X')
max++;
if(map[i][j]=='D')
{
di=i;
dj=j;
}
if(map[i][j]=='S')
{
si=i;
sj=j;
}
}
getchar();
}
if(n*m-max<=num)
{
printf("NO\n");
}
else
{
map[si][sj]='X';
judge=0;
dfs(si,sj,0);
if(judge==0)
printf("NO\n");
else
printf("YES\n");
}
}
return 0;
}
本文介绍了一个迷宫逃脱问题,通过深度优先搜索(DFS)解决狗仔如何在限定时间内找到出口的问题。迷宫由N*M的网格构成,地面会随时间消失,狗仔必须在门开启的瞬间到达。文章提供了两种实现方案的代码示例。
455

被折叠的 条评论
为什么被折叠?



