HDU 4308 - Saving Princess claire_

来源:互联网 发布:c语言入门书籍 pdf 编辑:程序博客网 时间:2024/05/05 22:11

 

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

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

                   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;}


 

 

 

 

原创粉丝点击