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

HDU 2128Tempter of the Bone II

編輯:關於C++

 

Tempter of the Bone II

Time Limit : 10000/5000ms (Java/Other) Memory Limit : 98304/32768K (Java/Other)
Total Submission(s) : 3 Accepted Submission(s) : 1
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

 

地圖上有炸彈,可以炸掉牆壁。問最少幾步能達到終點

狀態壓縮 地圖除了起點和終點,有62個點,可以用狀態壓縮保存拿去炸彈和炸掉牆的狀態

用容器標記,四維 狀態,坐標,炸彈數

 

 

#include
#include
#include
#include
#include
#include
#define LL unsigned long long
using namespace std;
int n,m;
char Map[10][10];
int Mp[10][10];
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
int len;
struct node
{
    int x,y;
    int t;
    int boom;
    unsigned long long v;
    friend bool operator < (node a,node b)
    {
        return a.t>b.t;
    }
    bool cheak()
    {
        if(x>=0&&x=0&&yv[8][8][9*8*8+1];//標記  當前點擁有的炸彈數的狀態  出現過則捨棄

bool cheak(node a)
{
    for(int i=0; iq;
    q.push(st);
    for(int i=0; i<8; i++)
        for(int j=0; j<8; j++)
            for(int l=0; l<8*8*9+1; l++)
                v[i][j][l].clear();
    v[st.x][st.y][st.boom].push_back(st.v);
    while(q.size())
    {
        st=q.top();
        q.pop();
        if(Map[st.x][st.y]=='D')
        {
            printf(%d
,st.t);
            return ;
        }
        for(int i=0; i<4; i++)
        {
            ed=st;
            ed.t++;
            ed.x+=dir[i][0];
            ed.y+=dir[i][1];
            if(ed.cheak())
            {
                if(Mp[ed.x][ed.y]>=0&&(ed.v&(1LL<0)
                        {
                            ed.boom--;
                            ed.t++;
                            ed.v|=(1LL<>n>>m)
    {
        if(n==0&&m==0)
            break;
        memset(Mp,-1,sizeof(Mp));
        len=0;
        for(int i=0; i>Map[i][j];
                if(Map[i][j]=='X'||Map[i][j]>='0'&&Map[i][j]<='9') Mp[i][j]=len++;
                else if(Map[i][j]=='S')
                {
                    st.x=i;
                    st.y=j;
                    st.t=0;
                    st.boom=0;
                    st.v=0;
                }
            }
        }
        bfs();
    }
    return 0;
}


 

 

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