Tempter of the Bone
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 125945 Accepted Submission(s): 33969
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.
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.
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
这道题可以不剪枝操作,也不会T了!
不懂剪枝的看这里:DFS中的奇偶剪枝学习笔记
这里说下不剪枝的技巧:为了避免多余的边界控制,可以从i=1,j=1开始读迷宫,在读之前将迷宫初始化为全部'X',即都为墙。这样在迷宫读取完毕后,周围就会自动出现一圈'X',这样就可以在搜索的时候只判断遇到'X'就return了。
这里贴一下深搜代码,不管剪不剪枝,这一段是可以不用修改的。
inline int DFS(int x,int y,int T)
{
if(mp[x][y]!='.'&&mp[x][y]!='S')//碰到X即为边界返回
return ;
if(T==)//剩一步时即可判断是否为出口,找到返回1
{
if(mp[x-][y]=='D')
return ;
if(mp[x+][y]=='D')
return ;
if(mp[x][y-]=='D')
return ;
if(mp[x][y+]=='D')
return ;
return ;
}
else
{
mp[x][y]='X';//标记走过
if(mp[x-][y]=='.'&&DFS(x-,y,T-))
return ;
if(mp[x+][y]=='.'&&DFS(x+,y,T-))
return ;
if(mp[x][y-]=='.'&&DFS(x,y-,T-))
return ;
if(mp[x][y+]=='.'&&DFS(x,y+,T-))
return ;
mp[x][y]='.';//还原走过
return ;
}
return ;
}
关于奇偶剪枝
首先举个例子,有如下4*4的迷宫,'.'为可走路段,'X'为障碍不可通过
S...
....
....
...D
从S到D的最短距离为两点横坐标差的绝对值+两点纵坐标差的绝对值 = abs(Sx - Dx) + abs(Sy - Dy) = 6,这个应该是显而易见的。
遇到有障碍的时候呢
S.XX
X.XX
...X
...D
你会发现不管你怎么绕路,最后从S到达D的距离都是最短距离+一个偶数,这个是可以证明的
而我们知道:
奇数 + 偶数 = 奇数
偶数 + 偶数 = 偶数
因此不管有多少障碍,不管绕多少路,只要能到达目的地,走过的距离必然是跟最短距离的奇偶性是一致的。
所以如果我们知道从S到D的最短距离为奇数,那么当且仅当给定的步数T为奇数时,才有可能走到。如果给定的T的奇偶性与最短距离的奇偶性不一致,那么我们就可以直接判定这条路线永远不可达了。
这里还有个小技巧,我们可以使用按位与运算来简化奇偶性的判断。我们知道1的二进制是1,而奇数的二进制最后一位一定是1,而偶数的二进制最后一位一定是0。所以如果数字&1的结果为1,那么数字为奇数,反之为偶数。
下面给出奇偶剪枝后的main函数代码:
int main()
{
int sx,sy,dx,dy;
int N,M,T;
while(scanf("%d%d%d",&N,&M,&T)&&N&&M&&T)
{
getchar();
memset(mp,'X',sizeof(mp));//把周围边界全部变成X
for(int i=;i<=N;i++)//从1开始读,留出边界位置
{
for(int j=;j<=M;j++)
{
scanf("%c",&mp[i][j]);
if(mp[i][j]=='S')
{
sx=i;
sy=j;
}
else if(mp[i][j]=='D')
{
dx=i;
dy=j;
}
}
getchar();
}
if((abs(sx-dx)+abs(sy-dy)-T)&)//奇偶剪枝,对1用按位与运算求奇偶
printf("NO\n");
else if(DFS(sx,sy,T)==)
printf("YES\n");
else printf("NO\n");
}
return ;
}
所以完整的AC代码如下:
#include <bits/stdc++.h>
using namespace std;
char mp[][];
inline int DFS(int x,int y,int T)
{
if(mp[x][y]!='.'&&mp[x][y]!='S')//碰到X即为边界返回
return ;
if(T==)//剩一步时即可判断是否为出口,找到返回1
{
if(mp[x-][y]=='D')
return ;
if(mp[x+][y]=='D')
return ;
if(mp[x][y-]=='D')
return ;
if(mp[x][y+]=='D')
return ;
return ;
}
else
{
mp[x][y]='X';//标记走过
if(mp[x-][y]=='.'&&DFS(x-,y,T-))
return ;
if(mp[x+][y]=='.'&&DFS(x+,y,T-))
return ;
if(mp[x][y-]=='.'&&DFS(x,y-,T-))
return ;
if(mp[x][y+]=='.'&&DFS(x,y+,T-))
return ;
mp[x][y]='.';//还原走过
return ;
}
return ;
}
int main()
{
int sx,sy,dx,dy;
int N,M,T;
while(scanf("%d%d%d",&N,&M,&T)&&N&&M&&T)
{
getchar();
memset(mp,'X',sizeof(mp));//把周围边界全部变成X
for(int i=;i<=N;i++)//从1开始读,留出边界位置
{
for(int j=;j<=M;j++)
{
scanf("%c",&mp[i][j]);
if(mp[i][j]=='S')
{
sx=i;
sy=j;
}
else if(mp[i][j]=='D')
{
dx=i;
dy=j;
}
}
getchar();
}
if((abs(sx-dx)+abs(sy-dy)-T)&)//奇偶剪枝,对1用按位与运算求奇偶
printf("NO\n");
else if(DFS(sx,sy,T)==)
printf("YES\n");
else printf("NO\n");
}
return ;
}