HDU 3619 BFS+优先队列

141 篇文章 0 订阅
13 篇文章 0 订阅

点击打开链接

题意:给一个地图,从S走到T,然后给了钥匙的位置,地图上数字点代表如果走这个点则要消耗数字的能量,而A到E是门,一个钥匙可以开一类门,问最少消耗多少能量就可以走到T

思路:对于钥匙来说,直接用状态压缩判断钥匙是否取过,然后因为是要走最小的花费,那么要用优先队列,没什么可以注意的,就是一个钥匙可以开一类门,而不是只能开一个门,注意着谢谢就应该能过,并不难的一道BFS

#include <queue>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3fll;
const int maxn=60;
int sx,sy,ex,ey,n,m,cnt,k;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
bool vis[maxn][maxn][40],vis1[maxn][maxn];
char str[maxn][maxn];
struct edge{
    int x,y,step,ss;
    char str1[maxn][maxn];
    friend bool operator< (edge n1,edge n2)
    {return n1.step>n2.step;}
};
struct snake{
    int x,y;
}sna[maxn];
int bfs(){
    priority_queue<edge>que;
    edge c,ne;
    memset(vis,0,sizeof(vis));
    str[sx][sy]='.';str[ex][ey]='.';
    c.x=sx,c.y=sy,c.step=0;c.ss=0;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            c.str1[i][j]='.';
        }
    }
    vis[c.x][c.y][0]=1;
    que.push(c);
    while(!que.empty()){
        c=que.top();que.pop();
        if(c.x==ex&&c.y==ey) return c.step;
        for(int i=0;i<4;i++){
            int xx=c.x+dir[i][0];
            int yy=c.y+dir[i][1];
            if(xx<0||xx>n-1||yy<0||yy>m-1||str[xx][yy]=='#') continue;
            if(vis[xx][yy][c.ss]) continue;
            ne.x=xx;ne.y=yy;ne.step=c.step;ne.ss=c.ss;
            for(int ll=0;ll<n;ll++) strcpy(ne.str1[ll],c.str1[ll]);
            if(vis1[xx][yy]){
                for(int i=0;i<cnt;i++){
                    if(xx==sna[i].x&&yy==sna[i].y){
                        if((ne.ss>>i)&1){
                            ne.step=c.step;
                        }else{
                            ne.ss+=(1<<i);
                        }
                        break;
                    }
                }
            }
            if(str[xx][yy]>='1'&&str[xx][yy]<='9'){
                ne.step=c.step+str[xx][yy]-'0';
            }
            if(str[xx][yy]>='A'&&str[xx][yy]<='E'){
                int op=str[xx][yy]-'A';
                if(((ne.ss>>op)&1)&&ne.str1[xx][yy]=='.') ne.str1[xx][yy]='#';
                else continue;
            }
            vis[ne.x][ne.y][ne.ss]=1;
            que.push(ne);
        }
    }
    return -1;
}
int main(){
    int T,cas=1,a,b;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d",&n,&m,&k);
        cnt=0;
        memset(vis1,0,sizeof(vis1));
        for(int i=0;i<n;i++) scanf("%s",str[i]);
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(str[i][j]=='S') sx=i,sy=j;
                if(str[i][j]=='T') ex=i,ey=j;
            }
        }
        for(int i=0;i<k;i++){
            scanf("%d%d",&a,&b);
            a--;b--;
            vis1[a][b]=1;
            sna[cnt].x=a;sna[cnt++].y=b;
        }
        int ans=bfs();
        printf("%d\n",ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值