HDU - 1010 Tempter of the Bone

来源:互联网 发布:如何开好淘宝直通车 编辑:程序博客网 时间:2024/05/16 03:43

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 5S.X...X...XD....3 4 5S.X...X....D0 0 0
 

Sample Output

NO

YES

题解:这道题就是寻找一条路径,这条是可以让狗狗生存的路径 == 给出的时间T, DFS题目,但是是要寻找相同时间的路径,所以加回溯的方法,如果这条路径不可以的就回去继续搜索。

剪枝的条件比较多,奇偶剪枝 http://baike.baidu.com/view/7789287.htm

下面主要说说奇偶性剪枝若有一迷宫,将迷宫的每一个位置有0或1表示(x+y为偶数时 为0 否则为1):0  1  0  1  01  0  1  0  10  1  0  1  01  0  1  0  1从图中我们可以很清晰的看出:任意一个位置周围相邻的必然是与本身值相反的值,也就是说,要想走到与本身相同的奇偶性点必然要走偶数步;同理,要想走到与本身相异的值的点必然要走奇数步;所以,1.当两个位置的奇偶性相同时(同为0或同为1)若时间为奇数  则必然无法到达;

2.当两个位置的奇偶性不同时(一个为1,另一个为0)若时间为偶数也必不能到达。

超过时间判断,'.'的数量<给出的时间no

#include <iostream>#include <cstdio>using namespace std;char pic[7][7];int t,n,m,start_x,start_y,end_x,end_y,block;bool flag;int direact[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};void dfs(int x,int y,int time){    if(flag == true)return;    if(x==end_x&&y==end_y&&time==t){flag = true;return;}    if(time>t)return;    if(pic[x][y] == 'X')return;    if(x<0||x>=n||y<0||y>=m)return;    for(int i = 0; i < 4; i++){        pic[x][y] = 'X';        dfs(x+direact[i][0],y+direact[i][1],time+1);        pic[x][y] = '.';    }}int main(){    while(scanf("%d%d%d",&n,&m,&t)!=EOF && n!=0 && m!=0 && t!=0){        block = 0;        for(int i = 0;i < n;i++){            scanf("%s",pic[i]);            for(int j = 0; j < m;j++){                if(pic[i][j] == 'X'){block++;continue;}                if(pic[i][j] == 'S'){start_x = i;start_y = j;continue;}                if(pic[i][j] == 'D'){end_x = i;end_y = j;}            }        }        if((end_x+end_y+start_x+start_y+t)%2==1){            printf("NO\n");continue;        }        if(n*m-block < t){            printf("NO\n");continue;        }        flag = false;        dfs(start_x,start_y,0);        flag == true?printf("YES\n"):printf("NO\n");    }    return 0;}


0 0
原创粉丝点击