题意就是给你一个图,从Y走到C,每次经过*都有一个固定的花费,Y是传送门,可以传送到任何其他的传送门,#是路障。输出最小的花费。
AC代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
const int maxn=5010;
int to[4][2]={1,0,-1,0,0,1,0,-1};
int r,c,tot,ans,cost;
bool vis[maxn][maxn];
char mp[maxn][maxn];
struct node{
int x,y;
int cnt;
};
int stx,sty,enx,eny;
node p[maxn*maxn],now,ne;
int bfs(int sx,int sy){
memset(vis,0,sizeof(vis));
ne.x=sx;ne.y=sy;ne.cnt=0;
vis[sx][sy]=1;
queue<node> q;
q.push(ne);
while(!q.empty()){
now=q.front();
// cout<<now.x<<" "<<now.y<<endl;
q.pop();
if(now.x==enx&& now.y==eny)
return now.cnt-1;
int nx,ny;
for(int i=0;i<=3;i++){
nx=now.x+to[i][0];
ny=now.y+to[i][1];
if(nx<=0 || ny<=0 || nx>r || ny>c || vis[nx][ny]==1)
continue;
if(mp[nx][ny]=='#')
continue;
vis[nx][ny]=1;
if(mp[nx][ny]=='P'){
for(int i=0;i<tot;i++){
if(nx==p[i].x&&ny==p[i].y)
continue;
ne.x=p[i].x;ne.y=p[i].y;
ne.cnt=now.cnt;
q.push(ne);
}
continue;
}
ne.x=nx;ne.y=ny;
ne.cnt=now.cnt+1;
q.push(ne);
}
}
return -1;
}
int main()
{
while(~scanf("%d%d%d",&r,&c,&cost)){
for(int i=1;i<=r;i++){
scanf("%s",mp[i]+1);
}
tot=0;
for(int i=1;i<=r;i++){
for(int j=1;j<=c;j++){
if(mp[i][j]=='Y')
{stx=i;sty=j;}
else if(mp[i][j]=='C')
{enx=i;eny=j;}
else if(mp[i][j]=='P')
{p[tot].x=i;p[tot++].y=j;}
}
}
int tmp=bfs(stx,sty);
if(tmp==-1)puts("Damn teoy!");
else
printf("%d\n",tmp*cost);
}
}