http://acm.hdu.edu.cn/showproblem.php?pid=4308
题目大意:
给你一张地图和一个cost,Y表示起点,C表示终点,*表示收费站,#表示通不过,P表示可以传送到任意P的传送点,求最小收费次数*cost。
解题思路:
由于地图没有空格,即不是YC#P的地方就会收费,bfs处理步数即可。
代码:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#define MAXN 5010
#define INF 0x7fffffff
#define eps 1e-10
#define MAX(x,y) ((x)>(y)? (x):(y))
#define MIN(x,y) ((x)<(y)? (x):(y))
#define MEM(a) (memset((a),0,sizeof(a)))
#define FRE freopen("input.txt","r",stdin)
using namespace std;
struct point
{
int x,y,step;
};
struct pos
{
int x,y;
}p[MAXN];
int r,c,cost;
char map[MAXN][MAXN];
int dx[4]={0,1,-1,0},dy[4]={1,0,0,-1};
int can_move(int a,int b)
{
if(a>=0&&a<r&&b>=0&&b<c&&map[a][b]) return 1;
else return 0;
}
int main()
{
while(scanf("%d%d%d",&r,&c,&cost)!=EOF)
{
int cnt=0;
__int64 ans=-1;
MEM(map);
queue<struct point>q;
struct point end;
for(int i=0;i<r;i++)
{
scanf("%s",map[i]);
for(int j=0;j<c;j++)
{
if(map[i][j]=='#') map[i][j]=0;
else if(map[i][j]=='*');
else if(map[i][j]=='P')
{
p[cnt].x=i; p[cnt++].y=j;
}
else if(map[i][j]=='Y')
{
map[i][j]=0;
struct point start;
start.x=i; start.y=j; start.step=0;
q.push(start);
}
else if(map[i][j]=='C')
{
end.x=i; end.y=j;
}
}
}
while(!q.empty())
{
struct point cur=q.front();
q.pop();
if(cur.x==end.x && cur.y==end.y) { ans=cur.step-1; break; }
for(int i=0;i<4;i++)
{
int tx=cur.x+dx[i],ty=cur.y+dy[i];
if(can_move(tx,ty))
{
struct point temp;
if(map[tx][ty]=='P')
{
for(int j=0;j<cnt;j++)
{
map[p[j].x][p[j].y]=0;
temp.x=p[j].x; temp.y=p[j].y;
temp.step=cur.step;
q.push(temp);
}
}
else
{
map[tx][ty]=0;
temp.x=tx; temp.y=ty; temp.step=cur.step+1;
q.push(temp);
}
}
}
}
if(ans!=-1) { ans=ans*cost; printf("%I64d\n",ans); }
else printf("Damn teoy!\n");
}
return 0;
}