HDU 1010 Tempter of the Bone (深度搜索+減枝優化)

Tempter of the Bone

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 55868    Accepted Submission(s): 15080



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
 

Author
ZHANG, Zheng
 

Source
 

Recommend
JGShining

題意:
一隻小狗想要逃出迷宮,他的當前位置在‘S’,如果能恰好經過t秒到達‘D’門逃出迷宮,輸出"YES",否則輸出“NO”。(被走過的空點不能重複走;‘X’爲牆,不能走,‘.’爲可走的路)
思路:
深度搜索+減枝優化
1.如果可走的空點比時間t要少,一定不能逃出,因爲不能重複走過的路。
2.奇偶減枝,從一個點到另一個點不管怎麼變化路徑,所走的步數都是奇數或都是偶數,那麼若所剩路徑的需要經過的最短時間和所剩時間不是同奇同偶,那麼一定不能逃出迷宮。

/*************************************************************************
	> File Name: I.cpp
	> Author: BSlin
	> Mail:  
	> Created Time: 2013年09月25日 星期三 21時24分49秒
 ************************************************************************/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iterator>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#define MP make_pair
#define INF (1<<30)
#define PI acos(-1.0)
#define esp 1e-8
const int dx[4]={0,0,0,0};
using namespace std;
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#if defined (_WIN32) || defined (__WIN32) || defined (WIN32) || defined (__WIN32__)
#define LL __int64
#define LLS "%" "I" "6" "4" "d"
#else
#define LL long long
#define LLS "%" "l" "l" "d"
#endif




char mark[10][10];
int to[4][2] = {-1,0,0,-1,1,0,0,1};
int n,m,t,stx,sty,enx,eny;
bool flag;
bool inmap(int x, int y) {
    if(x >= 1 && x <= n && y >= 1 && y <= m) return true;
    return false;
}

void dfs(int x,int y, int tm) {
    int i,nowx,nowy,temp;                  //nowx、nowy,這怎麼能設成全局變量呢
    if(flag == true) return;   //1.立即返回
    if(tm == t && x == enx && y == eny) {
        flag = true;
        return ;
    } 
    if(tm > t) return;
    if((abs(enx - x) + abs(eny - y)) % 2 != (t - tm) % 2) return ;   //2.奇偶減枝
    for(i=0; i<4; i++) {
        nowx = x + to[i][0];
        nowy = y + to[i][1];
        if(!inmap(nowx,nowy) || mark[nowx][nowy] == 'X') continue;
        mark[nowx][nowy] = 'X';    //標記訪問  
        dfs(nowx, nowy, tm + 1);
        mark[nowx][nowy] = '.';    //回溯取消標記  
    }
    return ;
}

int main(int argc, char** argv) {
    //read;
    int i,j,lu;
    while(scanf("%d %d %d\n",&n,&m,&t)) {
        if(n == 0 && m == 0 && t == 0) break;
        memset(mark,0,sizeof(mark));
        lu = 0;
        for(i=1; i<=n; i++) {
            for(j=1; j<=m; j++) {
                scanf("%c",&mark[i][j]);
                if(mark[i][j] == 'S') {
                    stx = i;
                    sty = j;
                    mark[i][j] = 'X';
                }
                else if(mark[i][j] == 'D') {
                    enx = i;
                    eny = j;
                    lu++;
                }
                else if(mark[i][j] == '.') {
                    lu++;
                }
            }
            getchar();
        }
        if(lu < t) {              //3.路程減枝
            printf("NO\n");
            continue;
        }
        flag = false;
        dfs(stx,sty,0);
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}




發佈了45 篇原創文章 · 獲贊 6 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章