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

hdu 4308 Saving Princess claire_(BFS+优先队列)

2017-03-07 20:15 134 查看


Saving Princess claire_

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2771    Accepted Submission(s): 1002


Problem Description

Princess claire_ was jailed in a maze by Grand Demon Monster(GDM) teoy.

Out of anger, little Prince ykwd decides to break into the maze to rescue his lovely Princess.

The maze can be described as a matrix of r rows and c columns, with grids, such as 'Y', 'C', '*', '#' and 'P', in it. Every grid is connected with its up, down, left and right grids.

There is only one 'Y' which means the initial position when Prince ykwd breaks into the maze.

There is only one 'C' which means the position where Princess claire_ is jailed.

There may be many '*'s in the maze, representing the corresponding grid can be passed through with a cost of certain amount of money, as GDM teoy has set a toll station.

The grid represented by '#' means that you can not pass it. 

It is said that as GDM teoy likes to pee and shit everywhere, this grid is unfortunately damaged by his ugly behavior.

'P' means it is a transmission port and there may be some in the maze. These ports( if exist) are connected with each other and Prince ykwd can jump from one of them to another. 

They say that there used to be some toll stations, but they exploded(surely they didn't exist any more) because of GDM teoy's savage act(pee and shit!), thus some wormholes turned into existence and you know the following things. Remember, Prince ykwd has his
mysterious power that he can choose his way among the wormholes, even he can choose to ignore the wormholes.

Although Prince ykwd deeply loves Princess claire_, he is so mean that he doesn't want to spend too much of his money in the maze. Then he turns up to you, the Great Worker who loves moving bricks, for help and he hopes you can calculate the minimum money he
needs to take his princess back.

 

Input

Multiple cases.(No more than fifty.)

The 1st line contains 3 integers, r, c and cost. 'r', 'c' and 'cost' is as described above.(0 < r * c <= 5000 and money is in the range of (0, 10000] )

Then an r * c character matrix with 'P' no more than 10% of the number of all grids and we promise there will be no toll stations where the prince and princess exist.

 

Output

One line with an integer, representing the minimum cost. If Prince ykwd cannot rescue his princess whatever he does, then output "Damn teoy!".(See the sample for details.)

 

Sample Input

1 3 3
Y*C

1 3 2
Y#C

1 5 2
YP#PC

 

Sample Output

3
Damn teoy!
0

 

Author

BUPT

 

Source

2012 Multi-University Training Contest 1

问题描述

公主claire_被囚禁在一个迷宫的大恶魔怪物(GDM)teoy。

出于愤怒,小王子ykwd决定闯入迷宫营救他可爱的公主。

迷宫可以被描述为r行和c列的矩阵,其中具有网格,诸如“Y”,“C”,“*”,“#”和“P”。每个网格都与其上,下,左和右网格相连。

只有一个“Y”意味着王子ykwd突破迷宫的初始位置。

只有一个“C”表示公主claire_被判入狱的地方。

在迷宫中可能有许多'*',代表相应的网格可以通过一定的金额的成本,因为GDM teoy已经设置收费站。

由'#'表示的网格意味着你不能通过它。

据说,由于GDM teoy喜欢撒尿和狗屎到处,这个网格不幸被他的丑陋行为损坏。

'P'表示它是一个传输端口,在迷宫中可能有一些。这些端口(如果存在)彼此连接,王子ykwd可以从其中一个跳到另一个。

他们说,以前有一些收费站,但他们爆炸(肯定是他们不存在),因为GDM teoy的野蛮行为(尿尿和屎!),因此一些虫洞变成了存在,你知道以下事情。记住,王子ykwd有他的神秘的力量,他可以选择他的方式在虫洞,即使他可以选择忽略虫洞。

虽然王子ykwd深深地爱公主claire_,他是如此的意思,他不想花太多的钱在迷宫。然后他转向你,伟大的工人谁爱移动砖,帮助,他希望你能计算他需要带回他的公主的最低金钱。

 

输入

多个案件(不超过五十个。)

第一行包含3个整数,r,c和cost。 'r','c'和'cost'如上所述(0 <r * c <= 5000,货币在(0,10000]的范围内)

然后一个r * c字符矩阵与'P'不超过所有网格的数量的10%,我们承诺,将没有收费站,王子和公主存在。

 

输出

一行带有整数,表示最小成本。如果王子王不能救他的公主,不管他做什么,然后输出“Damn teoy!”(详见示例)。

点击打开链接

题意:从Y走到C,#代表不能走,走*的话要花费C元,P是传送门可以到达任意一个P,问最小花费

思路:直接优先队列模拟一下就行,BFS搜一下,P直接记录,遇到了就判断它能到达的点能不能走就行了
//
//  main.cpp
//  dfs-bfs搜索
//
//  Created by liuzhe on 16/8/10.
//  Copyright © 2016年 my_code. All rights reserved.
//

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;

//hdu 4308
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fll;
const int maxn = 5005;
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 ii=0;ii<len;ii++)
{
pos tt = G[ii];
if(vis[tt.x][tt.y])
continue;
ne.x = tt.x;
ne.y = tt.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));//vector尾部添加元素
}
int ans = bfs();
if(ans == -1)
puts("Damn teoy!");
else
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  bfs