HDU 1010 Tempter of the Bone(搜索经典题)

来源:互联网 发布:男士商务单肩包 知乎 编辑:程序博客网 时间:2024/06/05 22:57

Tempter of the Bone

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


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 5S.X...X...XD....3 4 5S.X...X....D0 0 0
 

Sample Output
NOYES
 


 

题意:走出迷宫,S是起点,D是中点,X是墙无法通过,‘·’是路可以走。要求在n长m宽的矩阵迷宫里面在规定的t步走从S走到D是否可能。

典型的搜索。

要点分析:

典型的迷宫搜索,熟练掌握该题将具有里程碑式的意义!
每个block只能走一次
要求恰好某个给定的时间到达出口

剪枝条件:

如果可走的block的总数小于时间,将会产生什么情况?
还想到什么剪枝?


可以把map看成这样: 
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
1 0 1 0 1 0 
0 1 0 1 0 1 
从为 0 的格子走一步,必然走向为 1 的格子 
从为 1 的格子走一步,必然走向为 0 的格子 
即: 
 0 ->1或1->0 必然是奇数步 
 0->0 走1->1 必然是偶数步 

结论:
所以当遇到从 0 走向 0 但是要求时间是奇数的,或者, 从 1 走向 0 但是要求时间是偶数的 都可以直接判断不可达!


AC代码:

#include<stdio.h>#include<string.h>#include<stdlib.h>char map[9][9];int aws,n,m,t,di,dj;void dfs(int si,int sj,int cnt){     int i,temp;     if(si<=0||sj<=0||si>n||sj>m) return;//越界的不再执行      if(cnt==t&&si==di&&sj==dj) aws=1;//找到目的地      if(aws) return;//已经找到函数也不再执行           temp=(t-cnt)-abs(si-di)-abs(sj-dj); //剪枝     if(temp<0||temp&1) return;      if(map[si][sj+1]!='X')     {         map[si][sj+1]='X';         dfs(si,sj+1,cnt+1);         map[si][sj+1]='.';      }      if(map[si][sj-1]!='X')     {         map[si][sj-1]='X';         dfs(si,sj-1,cnt+1);         map[si][sj-1]='.';      }     if(map[si+1][sj]!='X')     {         map[si+1][sj]='X';         dfs(si+1,sj,cnt+1);         map[si+1][sj]='.';      }     if(map[si-1][sj]!='X')     {         map[si-1][sj]='X';         dfs(si-1,sj,cnt+1);         map[si-1][sj]='.';      }     return; }int main(){    int i,j,si,sj,wall;    while(scanf("%d %d %d",&n,&m,&t),n!=0&&m!=0&&t!=0)    {        wall=0;        getchar();        for(i=1;i<=n;i++)        {            for(j=1;j<=m;j++)            {               scanf("%c",&map[i][j]);               if(map[i][j]=='S') {si=i;sj=j;}               else if(map[i][j]=='D') {di=i;dj=j;}               else if(map[i][j]=='X') {wall++;}            }            getchar();        }        /*for(i=1;i<=n;i++)        {            for(j=1;j<=m;j++)            printf("%c ",map[i][j]);            puts("");        } */         if(n*m-wall<=t)//排除可走区域小于规定步数的不可能情况         {           printf("NO\n");           continue;        }        aws=0;        map[si][sj]='X';        dfs(si,sj,0);        if(aws) printf("YES\n");        else printf("NO\n");    }    return 0;} 


0 0
原创粉丝点击