A - Tempter of the Bone
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64Description
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
从S开始,走到D,恰好用走T步,且不重复,不过这道题实在太坑了,除了老是时间超限外,需要奇偶剪枝,输入也是要注意的地方。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char str[10][10];
int book[10][10];
int next[4][2] = {1,0,0,1,-1,0,0,-1};
int n,m,T,p,q,a,b,f;
void dfs(int x,int y,int step)
{
if(step > T || f) return;
if(step == T)
{
if(x == p && y == q)
f = 1;
return;
}
int temp=abs(x-p)+abs(y-q)-abs(step-T);//剪枝1:到时间了符合条件ok=1再退出,不符合条件直接退出。
if(temp>0 || temp%2) return;//temp大于0,说明步数不够到达终点,如果temp为负奇数,
//则要多走的步数不能够回到原路线上,不能准时到达终点
for(int i = 0; i<4; i++)
{
int tx = x+next[i][0];
int ty = y+next[i][1];
if(tx<1 || tx>n || ty<1 || ty>m)
continue;
if(book[tx][ty] == 0&&(str[tx][ty] == '.'||str[tx][ty] == 'D'))
{
book[tx][ty] = 1;
dfs(tx,ty,step+1);
book[tx][ty] = 0;
}
}
}
int main()
{
int i,j;
while(~scanf("%d%d%d",&n,&m,&T))
{
getchar();
if(!(n+m+T)) ;
for(i = 1; i<=n; i++)
{
for(j = 1; j<=m; j++)
{
scanf("%c",&str[i][j]);
if(str[i][j] == 'S')
{
a = i,b = j;
}
if(str[i][j] == 'D')
{
p = i,q = j;
}
}
getchar();
}
if(T<(p-a+q-b))//如果给出的步数小于要到达的最短路程
{
printf("NO\n");
continue;
}
f = 0;
memset(book,0,sizeof(book));
book[a][b] = 1;
dfs(a,b,0);
if(f) printf("YES\n");
else printf("NO\n");
}
return 0;
}
这样输入提交会错误,太坑了!
for(i = 1; i<=n; i++)
{
getchar();
for(j = 1; j<=m; j++)
{
scanf("%c",&str[i][j]);
if(str[i][j] == 'S')
{
a = i,b = j;
}
if(str[i][j] == 'D')
{
p = i,q = j;
}
}
}