程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU - 1010 Tempter of the Bone 深搜模板題(DPS)解題報告

HDU - 1010 Tempter of the Bone 深搜模板題(DPS)解題報告

編輯:C++入門知識

HDU - 1010 Tempter of the Bone 深搜模板題(DPS)解題報告


 

Tempter of the Bone

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



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

Author ZHANG, Zheng 題意:

 

給你一個N行M列的迷宮,X代表牆,.代表路,S代表起點,D代表終點,問能不能剛好在K秒從起點到達終點。

題解:

模板DFS,不需要剪枝。開一個二維數組表示向上下左右走,開一個oount記錄步數,若count>K,還沒走到就直接break。

參考代碼:

 

#include
#include
using namespace std;
char str[10][10];
int map[10][10],v[4][2]={0,1,0,-1,1,0,-1,0};
int n,m,k,i,j,flag,ax,ay,bx,by;
void dfs(int x,int y,int count)
{
    int mx,my;
    if(bx==x&&by==y)
    {
        if(k==count)
            flag=1;
        return;
    }
    if(count>=k)
        return;
    if(str[x][y]!='X')
    {
        for(int i=0;i<4;i++)
        {
            mx=x+v[i][0];
            my=y+v[i][1];
            if(mx>=1&&mx<=n&&my>=1&&my<=m&&!map[mx][my]&&str[mx][my]!='X')
            {
                map[mx][my]=1;
                dfs(mx,my,count+1);
                map[mx][my]=0;
                if(flag)
                   return;
            }
        }
    }
}
int main()
{
    
    while(scanf(%d%d%d,&n,&m,&k)!=EOF&&(n+m+k))
    {
        memset(map,0,sizeof(map));
        int count;
        for(i=1;i<=n;i++)
        {
            getchar();
            for(j=1;j<=m;j++)
            {
                scanf(%c,&str[i][j]);
                if(str[i][j]=='S')
                {
                    ax=i;
                    ay=j;
                }
                if(str[i][j]=='D')
                {
                    bx=i;
                    by=j;
                }
            }
        }
        getchar();
        if(abs(bx-ax)+abs(by-ay)>k||(ax+bx+ay+by+k)%2==1)
            printf(NO
);
        else 
        {
            flag=0;
            count=0;
            map[ax][ay]=1;
            dfs(ax,ay,count);
            if(flag)
                printf(YES
);
            else 
                printf(NO
);
        }
    }
    return 0;
} 


 

 

 

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