您的位置:首页 > 大数据 > 人工智能

hdu 4308 - Saving Princess claire_

2013-10-05 18:33 489 查看
题目:

Saving Princess claire_

题意:

王子救公主 , 蛋疼的王子希望支付最小的费用

给一张r*c的图,图上有'Y', 'C', '*', '#' 和 'P' , 各表示:

Y:王子的起始点

C:公主的地点,即终点

*:需要王子支付一定费用才能过 , 支付费用为cost

#:不能通行

P:传送点,每一个传送点可以无消耗直接通往任意一个其余传送点

普通移动无消耗

求救出公主的最小费用

思路:很简单的广搜 , 只是多了P,处理法:只要走上了P,就把所有的P相关位置全部压入队列,继续广搜就是了

代码:

//#pragma comment(linker, "/STACK:102400000,102400000")
#include "iostream"
#include "cstring"
#include "algorithm"
#include "cmath"
#include "cstdio"
#include "sstream"
#include "queue"
#include "vector"
#include "string"
#include "stack"
#include "cstdlib"
#include "deque"
#include "fstream"
#include "map"
using namespace std;
typedef long long LL;
const long long LINF = (long long)1e30;
const int INF = 522133279;
const int MAXN = 100000+100;
#define eps 1e-14
const int mod = 100000007;

char G[5010][5010];
bool vis[5010][5010];
int dir[4][2] = {1,0,0,1,-1,0,0,-1};

struct pr
{
int x;
int y;
LL cost;

pr(int _x = 0 , int _y = 0 , int _cost = 0)
: x(_x) , y(_y) , cost(_cost)
{}

bool operator < (const pr & b)const
{
return cost > b.cost;
}

}s,e;

vector<pr> place_Of_P;

int ok;
int r,c,cost;
LL minc;

LL minll(LL a , LL b)
{
return a > b ? b : a;
}

bool border(int x , int y)
{
return (x >= 0 && x < r) && (y >= 0 && y < c);
}

void bfs(pr s)
{
priority_queue<pr>  que;
que.push(s);
pr cur,tmp;
int x,y;

while(!que.empty())
{
cur = que.top();
que.pop();

for(int i = 0 ; i < 4 ; i++)
{
x = cur.x + dir[i][0];
y = cur.y + dir[i][1];

if(border(x,y) && !vis[x][y] && G[x][y] != '#')
{
vis[x][y] = 1;
tmp.x = x;
tmp.y = y;
tmp.cost = (G[x][y] == '*' ? cur.cost + cost: cur.cost);

if(G[x][y] == 'P')
for(int j = 0 ; j < place_Of_P.size() ; j++)
{
que.push(pr(place_Of_P[j].x , place_Of_P[j].y , tmp.cost));
vis[place_Of_P[j].x][place_Of_P[j].y] = 1;
}

else if(x == e.x && y == e.y)
{
minc = minll(minc , tmp.cost);
ok=1;
return ;
}
else
que.push(tmp);
}
}
}
}

int main()
{
//freopen("in","r",stdin);

while(cin >> r >> c >> cost)
{
memset(vis , 0 , sizeof(vis));
ok=0;
place_Of_P.clear();
minc = LINF;

for(int i = 0 ; i < r ; i++)
for(int j = 0 ; j < c ; j++)
{
cin >> G[i][j];
if(G[i][j] == 'Y')
{
vis[i][j]=1;
s.x = i;
s.y = j;
}
else if(G[i][j] == 'C')
{
e.x = i;
e.y = j;
}
else if(G[i][j] == 'P')
place_Of_P.push_back(pr(i,j,0));
}

bfs(s);

if(!ok)
cout << "Damn teoy!" << endl;
else
cout << minc << endl;
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdu bfs