Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 83008 Accepted Submission(s): 22619
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.
'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.
4 4 5 S.X. ..X. ..XD .... 3 4 5 S.X. ..X. ...D 0 0 0
NO YES
这题感觉有点坑 吃了不少WA和TLE 借鉴了他人的经验 后来多次检查发现 输入改成%s输入就对了
#include<stdio.h>
#include "math.h"
int vis[10][10];
char map[10][10];
int bx,by,heng,zong,val,leap=0;
int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
void dfs(int x,int y,int count)
{
int tempx,tempy,i;
vis[y][x]=1;
if(count>=val) return;
for(i=0;i<4;i++)
{
if(leap==1) return;
tempx=x+dir[i][0];
tempy=y+dir[i][1];
if(tempx>=0&&tempx<heng&&tempy>=0&&tempy<zong&&!vis[tempy][tempx])
if(map[tempy][tempx]=='D')
{
if(count==val-1) leap=1;
}
if(tempx>=0&&tempx<heng&&tempy>=0&&tempy<zong&&!vis[tempy][tempx])
if(map[tempy][tempx]=='.'){
dfs(tempx,tempy,count+1);
if(leap==0) vis[tempy][tempx]=0;
else return;
}
}
return ;
}
int main(void)
{
int i,j;
char str[10];
while(scanf("%d %d %d",&zong,&heng,&val)!=EOF&&(zong+heng+val))
{
int stx,sty;
leap=0;
for(i=0;i<zong;i++)
{
scanf("%s",str);
for(j=0;j<heng;j++)
{
map[i][j]=str[j];
vis[i][j]=0;
if(map[i][j]=='D'){
bx=j;
by=i;
}
else if(map[i][j]=='S'){
stx=j;
sty=i;
}
}
}
if(abs(stx-bx)+abs(sty-by)>val||(stx+bx+sty+by+val)%2==1)
{
printf("NO\n");
continue;
}
dfs(stx,sty,0);
if(leap==1) printf("YES\n");
else printf("NO\n");
}
}