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