HDU 1010

来源:互联网 发布:淘宝手机详情页文字 编辑:程序博客网 时间:2024/06/15 06:57


Tempter of the Bone
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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

NOYES

直接深搜会超时

由于是限制了走的步数可以用棋盘奇偶性定理剪枝

此题总共剪三枝

奇偶性

超时

缺少空间

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;char a[10][10];int n,m,t,enx,eny;int dir_x[]={-1,0,0,1};int dir_y[]={0,1,-1,0};int dis(int x,int y){    return abs(enx-x)+abs(eny-y);}bool dfs(int x,int y,int tim,int sum){    if(x<=0||y<=0||x>n||y>m||a[x][y]=='X') return 0;//边界    if(tim>t) return 0;//超时剪枝    if(sum<(t-tim)) return 0;//缺空间剪枝    if(a[x][y]=='D'&&tim==t) return 1;    char temp=a[x][y];a[x][y]='X';//准备释放用过的不合理路径    for(int i=0;i<4;i++)        if(dfs(x+dir_x[i],y+dir_y[i],tim+1,sum-1))            return 1;    a[x][y]=temp;    return 0;}int main(){    while(scanf("%d%d%d",&n,&m,&t)){        if(n==0&&m==0&&t==0) break;        getchar();        int stx,sty,sum;        for(int i=0;i<=n+1;i++)            for(int j=0;j<=m+1;j++)            a[i][j]='X';        for(int i=1;i<=n;i++){            for(int j=1;j<=m;j++){                scanf("%c",&a[i][j]);                if(a[i][j]=='S'){                    stx=i;sty=j;                    sum++;                }else if(a[i][j]=='D'){                    enx=i;eny=j;                    sum++;                }else if(a[i][j]=='.')                    sum++;            }            getchar();        }        if((dis(stx,sty)+t)%2==0&&dfs(stx,sty,0,sum))//奇偶剪枝            printf("YES\n");        else            printf("NO\n");    }}



0 0
原创粉丝点击