HDU 4308-saving princess Claire _

Source: Internet
Author: User

 

A huge string of text is actually useless.


Typical maze questions. The prince rescued the princess.

Y is King y Sub, C is the princess, p is the transfer array (can be instantly transmitted to any one for free), # Is the wall, * It is a toll station (it will cost money every time it passes through ).

Q: Can I save the princess? What is the minimum cost for saving the money?


No special P. priority queue is used directly...

Although the use of priority queue is a waste, but fortunately the code is the shortest. Fast.

 

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

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.