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
****************************************************************************************************************************************************************************************
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010
方法:DFS+奇偶剪枝。
每个block只能走一次,要求恰好在给定的T时间到达D点。
在diss和课件上面看见了奇偶剪枝,所以谓奇偶剪枝,直接上图:
ps:上图来自hdu某课件。
#include<cstdio>
#include<iostream>
using namespace std;
int t,m,n,dx,dy;
char Map[10][10];//图
bool run;
int dir[][2]={{0,-1},{0,1},{1,0},{-1,0}};//方向
void dfs(int rx,int ry,int rt){
int lx,ly,i,temp;
if(rx>n||ry>m||rx<1||ry<1) return;
if(rx==dx&&ry==dy&&rt==t) run=true;
if(run) return;
temp=t-rt-abs(rx-dx)-abs(ry-dy);
if(temp<0||temp&1) return;
for(i=0;i<4;i++){
lx=rx+dir[i][0],ly=ry+dir[i][1];
if(Map[lx][ly]!='X'){
Map[lx][ly]='X';
dfs(lx,ly,rt+1);
Map[lx][ly]='.';
}
}
return;
}
int main(){
int i,j,wall,x,y;
bool fs,fd;
while(scanf("%d%d%d",&n,&m,&t),n||m||t){
memset(Map,0,sizeof(Map));
wall=0;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
cin>>Map[i][j];
if(Map[i][j]=='S') x=i,y=j;
else if(Map[i][j]=='D') dx=i,dy=j;
else if(Map[i][j]=='X') wall++;
}
}
if(n*m-wall<=t){
puts("NO");
continue;
}
int temp=t-abs(x-dx)-abs(y-dy);//开始就判断一次
if(temp<0||temp&1){
puts("NO");
continue;
}
run=false;
Map[x][y]='X';
dfs(x,y,0);
if(run) puts("YES");
else puts("NO");
}
return 0;
}
代码中最容易错的地方就是,如果把13,14行组合在一起像些代码一样:
if(rx==dx&&ry==dy&&rt==t){
run=true;
return ;
}
这样反正苊是TLE了!