标签:
题意:从Y走到C,#代表不能走,走*的话要花费C元,P是传送门可以到达任意一个P,问最小花费
思路:直接优先队列模拟一下就行,BFS搜一下,P直接记录,遇到了就判断它能到达的点能不能走就行了,easy
#include <queue> #include <stdio.h> #include <iostream> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; typedef long long ll; typedef unsigned long long ull; const int inf=0x3f3f3f3f; const ll INF=0x3f3f3f3f3f3f3f3fll; const int maxn=5010; int n,m,k,sx,sy,ex,ey; int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; bool vis[maxn][maxn]; char str[maxn][maxn]; struct edge{ int x,y,step,flag; friend bool operator< (edge n1,edge n2) {return n1.step>n2.step;} }; struct pos{ int x,y; pos(int a,int b){x=a;y=b;} }; vector<pos>G; int bfs(){ priority_queue<edge>que; edge c,ne; memset(vis,0,sizeof(vis)); c.x=sx,c.y=sy,c.step=0; vis[c.x][c.y]=1; que.push(c); while(!que.empty()){ c=que.top();que.pop(); if(c.x==ex&&c.y==ey) return c.step; if(str[c.x][c.y]=='P'){ int len=G.size(); for(int ll=0;ll<len;ll++){ pos ttt=G[ll]; if(vis[ttt.x][ttt.y]) continue; ne.x=ttt.x;ne.y=ttt.y;ne.step=c.step; vis[ne.x][ne.y]=1; que.push(ne); } } for(int i=0;i<4;i++){ int xx=c.x+dir[i][0]; int yy=c.y+dir[i][1]; if(xx<0||xx>n-1||yy<0||yy>m-1||str[xx][yy]=='#') continue; if(vis[xx][yy]) continue; ne.x=xx;ne.y=yy;ne.step=c.step; if(str[xx][yy]=='*') ne.step+=k; vis[ne.x][ne.y]=1; que.push(ne); } } return -1; } int main(){ while(scanf("%d%d%d",&n,&m,&k)!=-1){ G.clear(); for(int i=0;i<n;i++) scanf("%s",str[i]); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(str[i][j]=='Y') sx=i,sy=j; if(str[i][j]=='C') ex=i,ey=j; if(str[i][j]=='P') G.push_back(pos(i,j)); } } int ans=bfs(); if(ans==-1) printf("Damn teoy!\n"); else printf("%d\n",ans); } return 0; }
标签:
原文地址:http://blog.csdn.net/dan__ge/article/details/51912471