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

HDU-2128-Tempter of the Bone II(BFS)

編輯:C++入門知識

HDU-2128-Tempter of the Bone II(BFS)


Problem Description The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze was changed and the way he came in was lost.He realized that the bone was a trap, and he tried desperately to get out of this maze.


The maze was a rectangle with the sizes of N by M. The maze is made up of a door,many walls and many explosives. Doggie need to reach the door to escape from the tempter. In every second, he could move one block to one of the upper, lower, left or right neighboring blocks. And if the destination is a wall, Doggie need another second explode and a explosive to explode it if he had some explosives. Once he entered a block with explosives,he can take away all of the explosives. Can the poor doggie survive? Please help him.
Input The input consists of multiple test cases. The first line of each test case contains two integers N, M,(2 <= N, M <= 8). which denote the sizes of the maze.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;
'S': the start point of the doggie;
'D': the Door;
'.': an empty block;
'1'--'9':explosives in that block.

Note,initially he had no explosives.

The input is terminated with two 0's. This test case is not to be processed.

Output For each test case, print the minimum time the doggie need to escape if the doggie can survive, or -1 otherwise.
Sample Input
4 4
SX..
XX..
....
1..D
4 4
S.X1
....
..XX
..XD
0 0

Sample Output
-1
9

Author XHD
Source HDU 2007-10 Programming Contest


思路:因為地圖會變化,結構體裡面要加一個數組保存地圖,標記的話就標記根據地圖的坐標和當前手中的炸藥數量來標記,但是同樣狀態的標記可能會有不同的走法,所以用整形變量來存,並且設定一個狀態可以訪問的閥值,這裡設為20。


#include 
#include 
#include 
using namespace std;

struct S{
int x,y,num,step;
char mp[8][8];
bool operator<(const S & p) const
{
    return step>p.step;
}
}t;

int nxt[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
char s[8][9];
int vis[8][8][577];

int main()
{
    int n,m,i,j,sx,sy,ex,ey;

    while(~scanf("%d%d",&n,&m) && n)
    {
        priority_queueque;

        for(i=0;i=0 && t.x=0 && t.y0)
                    {
                        vis[t.x][t.y][t.num]++;
                        t.step++;
                        t.num--;
                        t.mp[t.x][t.y]='.';
                        vis[t.x][t.y][t.num]++;

                        que.push(t);
                    }
                    else if(t.mp[t.x][t.y]>='1' && t.mp[t.x][t.y]<='9')
                    {
                        vis[t.x][t.y][t.num]++;
                        t.num+=t.mp[t.x][t.y]-'0';
                        t.mp[t.x][t.y]='.';
                        vis[t.x][t.y][t.num]++;

                        que.push(t);
                    }
                }
                 t=que.top();
            }
            que.pop();
        }

        if(que.empty()) printf("-1\n");
    }
}


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