题目链接:Tempter of the Bone
题目:
Tempter of the Bone
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.
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.
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秒这一瞬间从门口出来,要不他就会悲惨的死在里面。‘.’代表可以走,X代表不可以走。并且这个狗狗走过的路绝对不会走第二遍(好狗不走回头路);问这个狗狗能不能活着出来;
按照普通的dfs的写法:
void dfs(int x,int y,int t)
{
if(x<1||y<1||x>n||y>m) return ; //边界条件的判断,如果不满足则回到上一个点继续向周围扩散
if(mapp[x][y]=='D'&&t==T)//出口条件的判断
{
judge=true;
return ;
}
for(int i=0;i<4;i++)//传统套路,用dir[4][2]向周围扩散
{
if(judge) break;//如果前面的t==T,也就是说狗狗已经能出去了,就不需要其他路径的试探了
int xx=x+dir[i][0];
int yy=y+dir[i][1];
if(vist[xx][yy]==false&&mapp[xx][yy]!='X')//判断这个点是否符合条件
{
vist[xx][yy]=true;
dfs(xx,yy,t+1);
vist[xx][yy]=false;
}
}
}
这道题虽然dfs这样写能判断小狗是否能走出来,但是提交之后时间超限;所以要对其中的某些部分进行剪枝;
AC代码:
#include <bits/stdc++.h>
using namespace std;
int n,m,T,t;
char mapp[8][8];
bool vist[8][8];
int dir[4][2]={1,0,-1,0,0,1,0,-1};
int starx,stary,endx,endy;
bool judge;
void dfs(int x,int y,int t)
{
if(x<1||y<1||x>n||y>m) return ;
if(mapp[x][y]=='D'&&t==T)
{
judge=true;
return ;
}
//开始奇偶剪枝!
if((T-t)%2!=(abs(x-endx)+abs(y-endy))%2) return ;
/*前面代表如果能成功出去,那么从t到T所需时间(步数)的奇偶性
和这个点到出口的最短路径奇偶性相同*/
if(T-t-abs(x-endx)-abs(y-endy)<0) return ; //判断从此点所处的时间能否在T时刻到达出口 (T-t必须小于最短路径)
for(int i=0;i<4;i++)
{
if(judge) break;
int xx=x+dir[i][0];
int yy=y+dir[i][1];
if(vist[xx][yy]==false&&mapp[xx][yy]!='X')
{
vist[xx][yy]=true;
dfs(xx,yy,t+1);
vist[xx][yy]=false;
}
}
}
int main()
{
while(cin >>n>>m>>T&&n&&m&&T){
memset(mapp,'0',sizeof(mapp));
memset(vist,false,sizeof(vist));
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin >>mapp[i][j];
if(mapp[i][j]=='S')
starx=i,stary=j;
if(mapp[i][j]=='D')
endx=i,endy=j;
}
}
t=0;
vist[starx][stary]=true;
judge=false;
dfs(starx,stary,0);
if(judge)
cout <<"YES"<<endl;
else
cout << "NO"<<endl;
}
return 0;
}
剪枝中有两行代码:
//开始奇偶剪枝!
if((T-t)%2!=(abs(x-endx)+abs(y-endy))%2) return ;
/*前面代表如果能成功出去,那么从t到T所需时间(步数)的奇偶性
和这个点到出口的最短路径奇偶性相同*/
if(T-t-abs(x-endx)-abs(y-endy)<0) return ; //判断从此点所处的时间能否在T时刻到达出口 (T-t必须大于最短路径)
关于奇偶剪枝推链接:奇偶剪枝
简单说:
奇偶剪枝,就是步数的偏移量永远为偶数。
因此可以得出:最短路径步数+偏移量(偶数)=某一可行解歩数。
即最短路歩数和某一可行解歩数的奇偶性相同。
点点滴滴…