HDU - 1010 Tempter of the Bone

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

題解:這道題就是尋找一條路徑,這條是可以讓狗狗生存的路徑 == 給出的時間T, DFS題目,但是是要尋找相同時間的路徑,所以加回溯的方法,如果這條路徑不可以的就回去繼續搜索。

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

下面主要說說奇偶性剪枝 若有一迷宮,將迷宮的每一個位置有0或1表示(x+y爲偶數時 爲0 否則爲1): 0  1  0  1  0 1  0  1  0  1 0  1  0  1  0 1  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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章