Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 95088 Accepted Submission(s): 25799
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
卡时间特别严的一道题,做了整整一下午,看大神的思路,还是一直错,发现少了个getchar,真的醉了.
后来发现用自己的递归解法一直超时,真不知道问题出在哪.........算了,下次有机会再看吧........
题目详解:
http://acm.hdu.edu.cn/forum/read.php?tid=6158
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<queue>
using namespace std;
char map[55][55];
int dx[]={-1,1,0,0},dy[]={0,0,1,-1};
int n,m,t,vis[55][55],kase;
int bx,by,ex,ey;
void dfs(int x,int y,int s)
{
if(x<=0||x>n||y<=0||y>m||map[x][y]=='X'||s>t)//普通剪枝
{
return;
}
if(x==ex&&y==ey&&s==t)//寻找到目标
{
kase=1;
return;
}
int tp=t-s-abs(x-ex)+abs(y-ey);//传说中的奇偶剪枝
if(tp<0||tp&1)
{
return;
}
for(int i=0;i<4;++i)
{
int tx=x+dx[i],ty=y+dy[i],ts=s+1;
if(!vis[tx][ty])
{
vis[tx][ty]=1;
dfs(tx,ty,ts);//搜索下一个位置
if(kase)//符合直接跳出
{
return;
}
vis[tx][ty]=0;//递归回溯
}
}
return;
}
int main()
{
//freopen("shuju.txt","r",stdin);
while(scanf("%d%d%d",&n,&m,&t),n|m|t)
{
int cnt=0;
for(int i=1;i<=n;++i)
{
getchar();
for(int j=1;j<=m;++j)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='S')
{
bx=i;by=j;
}
else if(map[i][j]=='D')
{
ex=i;ey=j;
}
else if(map[i][j]=='X')
{
++cnt;
}
}
}
getchar();
if(cnt+t>=n*m)
{
printf("NO\n");
continue;
}
memset(vis,0,sizeof(vis));
vis[bx][by]=1;
kase=0;dfs(bx,by,0);
if(kase)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}
2016年1月29日
按着之前的大致思路,重新做了一遍,还是TLE了很多次,自己搜索学的不行,差的还远...
不过改了半天,总算过去了...
上面的代码 100+,这个代码700+...
#include<stdio.h>
#include<math.h>
#include<string.h>
using namespace std;
char map[15][15];
int n,m,t,bx,by,ex,ey,dx[4]={0,1,0,-1},dy[4]={1,0,-1,0},vis[15][15];
int dfs(int x,int y,int s)
{
if(s>t||x<0||x>=n||y<0||y>=m||map[x][y]=='X')
{
return 0;
}
int tp=t-s-abs(x-ex)-abs(y-ey);//成功至少需要走的步数
if(tp<0||tp&1)//不能小于而且肯定为偶数
{
return 0;
}
if(x==ex&&y==ey&&s==t)
{
return 1;
}
vis[x][y]=1;
for(int i=0;i<4;++i)
{
int tx=x+dx[i],ty=y+dy[i];
if(!vis[tx][ty]&&dfs(tx,ty,s+1))
{
return 1;
}
}
vis[x][y]=0;
return 0;
}
int main()
{
//freopen("shuju.txt","r",stdin);
while(scanf("%d%d%d",&n,&m,&t),n|m|t)
{
for(int i=0;i<n;++i)
{
scanf("%s",map[i]);
}
for(int i=0;i<n;++i)
{
for(int j=0;j<m;++j)
{
if(map[i][j]=='S')
{
bx=i;by=j;
}
else if(map[i][j]=='D')
{
ex=i;ey=j;
}
}
}
memset(vis,0,sizeof(vis));
if(dfs(bx,by,0))
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}