HDU 1010

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
杭电上的题,题意是从S点找到去D的路,D是一扇门并且在T秒倍的时候才会打开,
每一个格只能走一次,一开始肯定DFS搜,但是。。。超时了。。。。所以考虑剪枝
。最短路之外的所有多出的格子一定是成对出现的(奇偶剪枝)。在输入的时候再对
进行一些优化。可走路线小于时间T可以删除。
zouic#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#include<stdlib.h>
#include<map>
#include<vector>
#include<queue>
#include<set>
#include<ctype.h>
#define zuida 100000
using namespace std;
char zh[10][10];
int vis[10][10];
int tx[]={0,0,1,-1};
int ty[]={1,-1,0,0};
int N,M,T;
int xee,yee;
char pd(int x,int y)
{
    if(x>0&&x<=N&&y>0&&y<=M)
        return zh[x][y];
    else return '0';
}
int  dfs(int x,int y,int sum)
{
    int i,j,q;
    if((T-sum+1-abs(x-xee)-abs(y-yee)%2==1))return 0;
    for(q=0;q<4;q++)
            {
                if(pd(x+tx[q],y+ty[q])=='D'&&sum%T==0){return 1;}
                if(pd(x+tx[q],y+ty[q])=='.'&&vis[x+tx[q]][y+ty[q]]==0)
                    {
                        vis[x+tx[q]][y+ty[q]]=1;
                        if(dfs(x+tx[q],y+ty[q],sum+1)){return 1;}
                        vis[x+tx[q]][y+ty[q]]=0;
                    }
            }
            if(q==4){return 0;}
}
int main()
{
    int xe,ye;
    while(1)
    {
    memset(vis,0,sizeof(vis));
    cin>>N>>M>>T;
    if(!N&&!M&&!T)break;
    int i,j;
    int w=0;
    for(i=1;i<=N;i++)
    {
        getchar();
        for(j=1;j<=M;j++)
        {
            cin>>zh[i][j];
            if(zh[i][j]=='S'){xe=i;ye=j;}
            if(zh[i][j]=='D'){xee=i;yee=j;}
            if(zh[i][j]=='X'){w++;}
        }
    }
    if(N*M-w<T||(T-abs(xe-xee)-abs(ye-yee))%2==1)
    {
        cout<<"NO"<<endl;
        continue;
    }
    vis[xe][ye]=1;
    int k=1;
    if(dfs(xe,ye,k))cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    }
}

 

posted @ 2018-03-15 21:27  翛宁  阅读(144)  评论(0编辑  收藏  举报