hdu4771 Stealing Harry Potter’s Precious(状态压缩+bfs)

19 篇文章 0 订阅
3 篇文章 0 订阅

题意:给出一个图,求出经过所有给定点(<=4个)的最短路径。
题解:由于给定点很少,可以进行状态压缩,然后包里搜索即可。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std;
const int maxn = 100+10;
const int INF = 1e9+7;
int n,m,p;
int dp[maxn][maxn][1 << 4];
int mustmap[maxn][maxn];
struct Point {
    int row;
    int col;
    Point operator = (const Point &b) {
        row = b.row;
        col = b.col;
        return *this;
    }
};
Point must[maxn];
Point from;
struct HeapNode{
    Point a;
    int st;
    int step;
};
char map1[maxn][maxn];
char c;
int dx[] = {0,0,-1,1};
int dy[] = {1,-1,0,0};
queue<HeapNode> q;
void bfs(Point from ){
    HeapNode now;
    now.a = from;now.st=0;now.step = 0;
    q.push(now);
    while(!q.empty()){
        HeapNode ne = q.front();q.pop();
        Point np = ne.a;
        int nr = np.row;
        int nc = np.col;
        int nstep = ne.step;
        for(int i=0;i<4;i++){
            int nowr=  nr + dx[i];
            int nowc = nc + dy[i];
            int nowst = ne.st;
            if(nowr<=0 || nowr >n || nowc<=0 || nowc >m ) continue;
            if(map1[nowr][nowc] == '#') continue;
            if(mustmap[nowr][nowc] > 0) {//printf("nowr: %d  nowc: %d\n",nowr,nowc);
                int temp  = mustmap[nowr][nowc];
                if(nowst & (1 << (temp-1)));
                else nowst ^= (1 << (temp-1));
            }
            if(dp[nowr][nowc][nowst] == -1) {
                dp[nowr][nowc][nowst] = nstep + 1;
                q.push((HeapNode){(Point){nowr,nowc},nowst,nstep+1});
            }
        }
    }
}
int main()
{
    freopen("G.in","r",stdin);
    freopen("G.out","w",stdout);
    while(~scanf("%d%d",&n,&m) && n){//printf("1\n");
        memset(mustmap,0,sizeof(mustmap));
        memset(dp,-1,sizeof(dp));
        scanf("%c",&c);
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++) {
                scanf("%c",&map1[i][j]);
                if(map1[i][j] == '@') {from.row = i;from.col = j;}
            }
            scanf("%c",&c);
        }
        scanf("%d",&p);
        for(int i=1;i<=p;i++) {
            scanf("%d%d",&must[i].row,&must[i].col);
            mustmap[must[i].row][must[i].col] = i;
        }
        bfs(from);
        int ans = INF;
        for(int i=1;i<=p;i++) {
                Point &e = must[i];
            if(dp[e.row][e.col][(1 << p) - 1] != -1)
                ans = min(ans,dp[e.row][e.col][(1 << p) - 1]);
        }
        if(ans == INF) ans = -1;
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值