自第一次接触搜索到现在一年多了,搜索一直是我引以为傲的,没想到今天栽在这一题上
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010
Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 70373 Accepted Submission(s): 19403
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
AC代码:
<pre name="code" class="cpp"><pre name="code" class="cpp"><span style="color:#362e2b;">#include<stdio.h>
#include<string.h>
int map[10][10],n,m,X,Y;
short mark[10][10],Mark;
int Abs(int x)
{
return x>0?x:-x;
}
void visit(int x,int y,int t)
{
int temp;
if(Mark)return;
if(x==X&&y==Y&&t==0){Mark=1;return;}
temp=t-Abs(x-X)-Abs(y-Y);
if(temp<0||temp&1)return;//非常重要的剪枝
/*********
*就是当前剩下的时间与当前位置到目的地的距离的差如果小于0
*则肯定没办法到达
*如果为奇数,一样不可能在要求的时间到达
************************************************************/
if(map[x-1][y]&&mark[x-1][y]==0&&t)//向上
{
mark[x-1][y]=1;
visit(x-1,y,t-1);
mark[x-1][y]=0;
}
if(map[x+1][y]&&mark[x+1][y]==0&&t)//向下
{
mark[x+1][y]=1;
visit(x+1,y,t-1);
mark[x+1][y]=0;
}
if(map[x][y-1]&&mark[x][y-1]==0&&t)//向左
{
mark[x][y-1]=1;
visit(x,y-1,t-1);
mark[x][y-1]=0;
}
if(map[x][y+1]&&mark[x][y+1]==0&&t)//向右
{
mark[x][y+1]=1;
visit(x,y+1,t-1);
mark[x][y+1]=0;
}
}
int main()
{
int i,j,x,y,t;
char c;
while(</span><strong><span style="color:#ff0000;">scanf("%d%d%d%*c",&n,&m,&t)</span></strong><span style="color:#362e2b;">,n+m+t)
{
Mark=0;
memset(map,0,sizeof(map));
memset(mark,0,sizeof(mark));
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
scanf("%c",&c);
if(c=='.')map[i][j]=1;
else if(c=='S'){x=i;y=j;}
else if(c=='D'){X=i;Y=j;map[i][j]=1;}
}
</span><strong><span style="color:#ff0000;">getchar();</span></strong><span style="color:#362e2b;">
}
visit(x,y,t);
printf(Mark?"YES\n":"NO\n");
}
return 0;
}
</span>
WA代码:
<pre name="code" class="cpp">#include<stdio.h>
#include<string.h>
int map[10][10],n,m,X,Y;
short mark[10][10],Mark;
int Abs(int x)
{
return x>0?x:-x;
}
void visit(int x,int y,int t)
{
int temp;
if(Mark)return;
if(x==X&&y==Y&&t==0){Mark=1;return;}
temp=t-Abs(x-X)-Abs(y-Y);
if(temp<0||temp&1)return;//非常重要的剪枝
if(map[x-1][y]&&mark[x-1][y]==0&&t)//向上
{
mark[x-1][y]=1;
visit(x-1,y,t-1);
mark[x-1][y]=0;
}
if(map[x+1][y]&&mark[x+1][y]==0&&t)//向下
{
mark[x+1][y]=1;
visit(x+1,y,t-1);
mark[x+1][y]=0;
}
if(map[x][y-1]&&mark[x][y-1]==0&&t)//向左
{
mark[x][y-1]=1;
visit(x,y-1,t-1);
mark[x][y-1]=0;
}
if(map[x][y+1]&&mark[x][y+1]==0&&t)//向右
{
mark[x][y+1]=1;
visit(x,y+1,t-1);
mark[x][y+1]=0;
}
}
int main()
{
int i,j,x,y,t;
char c;
while(<strong><span style="color:#ff0000;">scanf("%d%d%d",&n,&m,&t)</span></strong>,n+m+t)
{
Mark=0;
memset(map,0,sizeof(map));
memset(mark,0,sizeof(mark));
for(i=1;i<=n;i++)
{
<strong><span style="color:#ff0000;">getchar();</span></strong>
for(j=1;j<=m;j++)
{
scanf("%c",&c);
if(c=='.')map[i][j]=1;
else if(c=='S'){x=i;y=j;}
else if(c=='D'){X=i;Y=j;map[i][j]=1;}
}
}
visit(x,y,t);
printf(Mark?"YES\n":"NO\n");
}
return 0;
}
注意我加粗的部分,该题输入部分,某行可能有多余的空格