HDU 2128Tempter of the Bone II(bfs + 保存每一步的图)

Tempter of the Bone II
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 98304/32768 K (Java/Others)
Total Submission(s): 1830 Accepted Submission(s): 486

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

Recommend
威士忌 | We have carefully selected several similar problems for you: 1401 1548 1983 1254 1372

题意:小狗要逃离迷宫,迷宫是有墙的,有的点是有炸弹的,可以收集炸弹,炸开墙,正是由于这个原因,所以用vis[cnt][x][y],(cnt是点到(x,y)的炸弹数)无法标记,还需要每一步可能走的图的状态

#include<bits/stdc++.h>
#define cl(a,b) memset(a,b,sizeof(a));
#define LL long long
#define out(x) cout<<x<<endl;
using namespace std;
const int maxn=10;
const int inf=9999999;
int n,m;
int si,sj,ei,ej;
int dir[][2]={{1,0},{0,1},{-1,0},{0,-1}};
char a[maxn][maxn];
int vis[1005][maxn][maxn];
struct node{
    int step,cnt;//步数,炸弹数
    int x,y;//坐标
    char a[maxn][maxn];//每一步对应的图
    node(int a,int b,int c,int d){
        step=a;cnt=b;x=c;y=d;
    }
    bool friend operator<(node a,node b){
        return a.step>b.step;
    }
};
bool pan(node s){
    if(s.x<0||s.x>=n||s.y<0||s.y>=m)return false;
    return true;
}

int bfs(){
    cl(vis,0);
    priority_queue<node> q;
    node t=node(0,0,si,sj);
    memcpy(t.a,a,sizeof(a));
    vis[0][si][sj]=1;
    q.push(t);
    while(!q.empty()){
        node s=q.top();q.pop();
        for(int i=0;i<4;i++){
            node tmp=s;
            tmp.x+=dir[i][0];
            tmp.y+=dir[i][1];
            tmp.step++;
            if(pan(tmp)&&vis[tmp.cnt][tmp.x][tmp.y]<50){
                char op=tmp.a[tmp.x][tmp.y];
                if(tmp.x==ei&&tmp.y==ej){
                    return tmp.step;
                }
                else if(op=='.'){
                    vis[tmp.cnt][tmp.x][tmp.y]++;
                    q.push(tmp);
                }
                else if(op=='X'&&tmp.cnt>0){
                    vis[tmp.cnt][tmp.x][tmp.y]++;
                    tmp.step++;
                    tmp.cnt--;
                    tmp.a[tmp.x][tmp.y]='.';
                    q.push(tmp);
                }
                else if(op>='1'&&op<='9'){
                    vis[tmp.cnt][tmp.x][tmp.y]++;
                    tmp.cnt+=op-'0';
                    tmp.a[tmp.x][tmp.y]='.';
                    q.push(tmp);
                }
            }
        }
    }
    return -1;
}

int main(){

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

        for(int i=0;i<n;i++){
            scanf("%s",a[i]);
            for(int j=0;j<m;j++){
                if(a[i][j]=='S'){
                    si=i;sj=j;
                    a[i][j]='.';
                }
                else if(a[i][j]=='D'){
                    ei=i;ej=j;
                    a[i][j]='.';
                }
            }
        }

        cout<<bfs()<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值