HDU 4308 BFS+优先队列

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

点击打开链接

题意:从Y走到C,#代表不能走,走*的话要花费C元,P是传送门可以到达任意一个P,问最小花费

思路:直接优先队列模拟一下就行,BFS搜一下,P直接记录,遇到了就判断它能到达的点能不能走就行了,easy

#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=5010;
int n,m,k,sx,sy,ex,ey;
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
bool vis[maxn][maxn];
char str[maxn][maxn];
struct edge{
    int x,y,step,flag;
    friend bool operator< (edge n1,edge n2)
    {return n1.step>n2.step;}
};
struct pos{
    int x,y;
    pos(int a,int b){x=a;y=b;}
};
vector<pos>G;
int bfs(){
    priority_queue<edge>que;
    edge c,ne;
    memset(vis,0,sizeof(vis));
    c.x=sx,c.y=sy,c.step=0;
    vis[c.x][c.y]=1;
    que.push(c);
    while(!que.empty()){
        c=que.top();que.pop();
        if(c.x==ex&&c.y==ey) return c.step;
        if(str[c.x][c.y]=='P'){
            int len=G.size();
            for(int ll=0;ll<len;ll++){
                pos ttt=G[ll];
                if(vis[ttt.x][ttt.y]) continue;
                ne.x=ttt.x;ne.y=ttt.y;ne.step=c.step;
                vis[ne.x][ne.y]=1;
                que.push(ne);
            }
        }
        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]) continue;
            ne.x=xx;ne.y=yy;ne.step=c.step;
            if(str[xx][yy]=='*') ne.step+=k;
            vis[ne.x][ne.y]=1;
            que.push(ne);
        }
    }
    return -1;
}
int main(){
    while(scanf("%d%d%d",&n,&m,&k)!=-1){
        G.clear();
        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]=='Y') sx=i,sy=j;
                if(str[i][j]=='C') ex=i,ey=j;
                if(str[i][j]=='P') G.push_back(pos(i,j));
            }
        }
        int ans=bfs();
        if(ans==-1) printf("Damn teoy!\n");
        else printf("%d\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值