程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> HDU_1010 Tempter of the Bone[DFS]

HDU_1010 Tempter of the Bone[DFS]

編輯:關於C++

傳送門:1010

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

思路:dfs搜索。此題考剪枝,dis=(abs(ex-cx)+abs(ey-cy))是到D點最近的距離,(t-tc)-dis是要多走的距離,由題意不能斜走,所以要多走的距離一定時偶數。


代碼:

/************************************************
* Author: Ac_sorry
* File:
* Create Date:
* Motto: One heart One life
* CSDN: http://blog.csdn.net/code_or_code
*************************************************/

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define seed_ 131
#define eps 1e-8
#define mem(a,b) memset(a,b,sizeof a)
#define w(i) tree[i].w
#define ls(i) tree[i].ls
#define rs(i) tree[i].rs
using namespace std;
typedef long long LL;

const int N=50010;
int n,m,sx,sy,ex,ey,t;
char gra[30][30];
int vis[30][30];
int go[4][2]={{0,1},{0,-1},{-1,0},{1,0}};
int ok;

void dfs(int cx,int cy,int ct)
{

    int tmp=(t-ct)-(abs(ex-cx)+abs(ey-cy));
    if(ok||ct>t||tmp<0||tmp%2) return;
    if(ct==t&&cx==ex&&cy==ey){
        ok=1;
        return;
    }
    int x,y;
    for(int i=0;i<4;i++)
    {
        x=cx+go[i][0];
        y=cy+go[i][1];
        if(x>=0&&x=0&&y>n>>m>>t)
    {
        if(m==0&&n==0&&t==0)
            break;
        ok=0;
        for(int i=0;i



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved