閱讀761 返回首頁    go 技術社區[雲棲]


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

Sample Output

NO YES
 


 

題意:走出迷宮,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;
} 


最後更新:2017-04-03 12:55:01

  上一篇:go JAVA中URL鏈接中文參數亂碼處理方法
  下一篇:go iOS應用的crash日誌的分析基礎