HDU 4308 BFS+优先队列

点击打开链接

题意:从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;
}
时间: 2024-08-01 12:15:47

HDU 4308 BFS+优先队列的相关文章

Saving Princess claire_(hdu 4308 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2305    Accepted Submission(s): 822 Problem Description Princess claire_ wa

HDU 2822 (BFS+优先队列)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同,应该先搜cost小的.直接退化为最短路问题. 优先队列优化. 卡输入姿势.如果O(n^2)逐个读的话会T掉.要用字符串读一行. #include "cstdio" #include "queue" #include "cstring" using

HDU 3619 BFS+优先队列

点击打开链接 题意:给一个地图,从S走到T,然后给了钥匙的位置,地图上数字点代表如果走这个点则要消耗数字的能量,而A到E是门,一个钥匙可以开一类门,问最少消耗多少能量就可以走到T 思路:对于钥匙来说,直接用状态压缩判断钥匙是否取过,然后因为是要走最小的花费,那么要用优先队列,没什么可以注意的,就是一个钥匙可以开一类门,而不是只能开一个门,注意着谢谢就应该能过,并不难的一道BFS #include <queue> #include <stdio.h> #include <ios

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

hdu 1242 Rescue(bfs+优先队列)

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approach Angel. We assume

hdu 1242 Rescue (BFS+优先队列)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1242 这道题目我是用BFS+优先队列做的.听说只用bfs会超时. 因为这道题有多个营救者,所以我们从被营救者开始bfs,找到最近的营救者就是最短时间. 先定义一个结构体,存放坐标x和y,还有到达当前点(x,y)消耗的时间. struct node { int x,y; int time; friend bool operator < (const node &a,const node &

hdu 2102 A计划 详细题解 (BFS+优先队列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 开始看到四分之一的AC率感觉有点吓人,后来一做感觉就是模板改了点东西而已,一遍就AC了,不过在主函数和全局变量里面都定义了n和m导致我白白浪费了debug的时间.果然全局变量得小心用啊. 跟模板一样的,定义一个结构体,只不过多加了个参数,就是迷宫的层数,我用0代表第一层,1代表第二层,这在数组里面会体现的. struct node { int index;//层数

HDU 1242 Rescue(优先队列+bfs)

题目地址:HDU 1242 这个题相比于普通的bfs有个特殊的地方,经过士兵时会额外消耗时间,也就是说此时最先搜到的时候不一定是用时最短的了.需要全部搜一遍才可以.这时候优先队列的好处就显现出来了.利用优先队列,可以让队列中的元素按时间排序,让先出来的总是时间短的,这样的话,最先搜到的一定是时间短的,就不用全部搜一遍了.PS:我是为了学优先队列做的这题..不是为了这题而现学的优先队列.. 代码如下: #include <iostream> #include <stdio.h> #i

hdu 3345 War Chess (bfs+优先队列)

War Chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1732    Accepted Submission(s): 416 Problem Description War chess is hh's favorite game: In this game, there is an N * M battle map, an