HDU 4308 - Saving Princess claire_

22 篇文章 0 订阅

 

 很大一串文字,其实都没啥用。

              很典型的迷宫题。王子救公主。

                   Y是王Y子,C是公主,P是传送阵(可免费瞬间传送到任何一个P),#是墙,*是收费站(每次经过*都要花费金钱)。

                   问能不能救到公主,能救的话,最少花费是多少?

              没特判P,直接用优先队列过的。。。 

                  虽然用优先队列有点浪费,但好在代码最短。敲的快。

 

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cmath>

using namespace std;

struct point{
    int x,y;
}p[1000];
struct path{
    int x,y,cost;
    path(){};
    path(int a,int b,int c){
        x=a;
        y=b;
        cost=c;
    }
    bool friend operator<(path p,path q){
        return p.cost>q.cost;
    }
};

priority_queue<path>que;

int n,m,cost;
int fx[]={1,-1,0,0};
int fy[]={0,0,1,-1};
string map[5004];
int yx,yy;

int cnt;

int bfs(){
    int k,i,j;
    path no,nx;
    while(!que.empty()) que.pop();
    que.push(path(yx,yy,0));
    map[yx][yy]='#';
    while(!que.empty()){
        no=que.top();
        que.pop();
        for(k=0;k<4;k++){
            nx.x=no.x+fx[k];
            nx.y=no.y+fy[k];
            if(nx.x<0 || nx.x>=n || nx.y<0 || nx.y>=m) continue;
            if(map[nx.x][nx.y]=='#') continue;
            if(map[nx.x][nx.y]=='P'){
                for(i=0;i<cnt;i++){
                    que.push(path(p[i].x,p[i].y,no.cost));
                    map[p[i].x][p[i].y]='#';
                }
            }
            else{
                if(map[nx.x][nx.y]=='C')
                    return no.cost;
                else{
                    map[nx.x][nx.y]='#';
                    que.push(path(nx.x,nx.y,no.cost+cost));
                }
            }
        }
    }
    return -1;
}


int main(){
    int i,j,res;
    while(cin>>n>>m>>cost){
        cnt=0;
        for(i=0;i<n;i++){
            cin>>map[i];
            for(j=0;j<m;j++)
                if(map[i][j]=='Y')
                    yx=i,yy=j;
                else if(map[i][j]=='P'){
                    p[cnt].x=i;
                    p[cnt].y=j;
                    cnt++;
                }
        }
        res=bfs();
        if(res==-1)
            cout<<"Damn teoy!"<<endl;
        else
            cout<<res<<endl;
    }
    return 0;
}


 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值