HDU2128 (dfs)Tempter of the Bone II

           我是题目快戳我

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.
InputThe 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. 
OutputFor 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

题意:此题要求从S走到D,'X'是墙,'.'是路,'1'到'9'是炸弹的数量,墙可以消耗一个炸弹炸掉,同时需要1个单位的时间来安放炸弹,问所花费的最少时间是多少;

思路:地图不大,炸弹的数量也不多,所以开一个3维的标记数组vis,vis[i][j][k]表示坐标i,j,所携带的炸弹数量为k时,是否走过;

同时再开个辅助数组用来剪枝,详情看代码:

#include<bits/stdc++.h>
using namespace std;
char s[10][10];
bool vis[10][10][640];
typedef pair<int,int>P;
P v[10][10];//记录到坐标x,y的最短时间和炸弹数量
int n, m, l;
int d[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};
void dfs(int x, int y, int z, int zt) {
    //cout<<x<<" "<<y<<" "<<z<<" "<<zt<<endl;
    if(s[x][y] == 'D') {
        if(z < l)
            l = z;
        return ;
    }
    if(z >= l) {
        return ;
    }
    if(v[x][y].first>z){//有更优解更新答案
        v[x][y].first=z;
        v[x][y].second=zt;
    }
    else if(v[x][y].first<z){//时间花的多还炸弹少肯定是不需要的
        if(zt<=v[x][y].second)return ;
    }
    else if(v[x][y].first==z){//时间相同,炸弹也相同说明重复了,炸弹少的话说明有更优状态不要继续搜下去
        if(v[x][y].second<zt)v[x][y].second=zt;
        else return ;
    }
    for(int i = 0; i < 4; i++) {
        int xx = x + d[i][0];
        int yy = y + d[i][1];
        if(xx < 0 || xx >= n || yy < 0 || yy >= m)
            continue;
        if(vis[xx][yy][zt])
            continue;
        vis[xx][yy][zt] = 1;
        if(s[xx][yy] == '.' || s[xx][yy] == 'D' || s[xx][yy] == 'S') {
            dfs(xx, yy, z + 1, zt);
        }
        if(s[xx][yy] == 'X') {
            if(zt>0) {
                vis[xx][yy][zt - 1] = 1;
                s[xx][yy] = '.';
                dfs(xx, yy, z + 2, zt - 1);
                s[xx][yy] = 'X';
                vis[xx][yy][zt - 1] = 0;
            }
        }
        if(s[xx][yy] >= '1' && s[xx][yy] <= '9') {
            int a = s[xx][yy] - '0';
            vis[xx][yy][zt + a] = 1;
            s[xx][yy] = '.';
            dfs(xx, yy, z + 1, zt + a);
            vis[xx][yy][zt + a] = 0;
            s[xx][yy] = '0' + a;
        }
        vis[xx][yy][zt] = 0;
    }
    return ;
}
int main() {
    while(scanf("%d%d", &n, &m) == 2) {
        if(n == m && n == 0)
            break;
        l = 1000000000;
        memset(vis,0,sizeof(vis));
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
            v[i][j].first=1000000000,v[i][j].second=0;
        for(int i = 0; i < n; i++)
            scanf("%s", s[i]);
        for(int i = 0; i < n; i++)
            for(int j = 0; j < m; j++)
                if(s[i][j] == 'S') {
                    vis[i][j][0] = 1;
                    dfs(i, j, 0, 0);
                }
        if(l!=1000000000)printf("%d\n",l);
        else printf("-1\n");
    }
    return 0;
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值