hdu 4308 Saving Princess claire BFS加优先队列

本文介绍了一个迷宫救援问题,通过BFS算法寻找从起点到终点的最经济路径。迷宫包含墙壁、传送门等元素,需计算最低花费。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



Problem I

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 628Accepted Submission(s): 173
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.)
SampleInput
1 3 3
Y*C
1 3 2
Y#C
1 5 2
YP#PC
SampleOutput
3
Damn teoy!
0

题意: Y开始点 C 终止点 P 传送门 与所有传送门连同 #墙 *能走的地方 但是要钱 只有*要钱

输入 行数 列数 以及钱

问花的最少的钱是多少

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
using namespace std;
int hang,lie,val,cnt;
char map[5100][5100];
struct haha
{
int x;
int y;
int val;
friend bool operator<(struct haha a,struct haha b)
{
return a.val>b.val;
}
}q,temp;
struct p
{
int x;
int y;
}a[5000+10];
int step[4][2]={1,0,-1,0,0,1,0,-1},s_x,s_y,e_x,e_y;//把坐标存成了字符型 艾 一天全是低级错误 脑子不清醒
int BFS()
{
int i,j,xx,yy,ans=999999999;
priority_queue<haha>que;
q.x=s_x;
q.y=s_y;///////// 已开始这里写成了e_y 想打自己啊
q.val=0;
que.push(q);
map[s_x][s_y]='#';
while(!que.empty())
{
temp=que.top();
que.pop();
for(i=0;i<4;i++)
{
xx=temp.x+step[i][0];
yy=temp.y+step[i][1];
if(xx>=0&&xx<hang&&yy>=0&&yy<lie&&map[xx][yy]!='#')
{
if(xx==e_x&&yy==e_y)
{
if(ans>temp.val) ans=temp.val;continue;
}
if(map[xx][yy]=='*')
{
map[xx][yy]='#';
q.x=xx;
q.y=yy;
q.val=temp.val+val;//////这里忘记加temp.val MLE了
que.push(q);
continue;
}
if(map[xx][yy]=='P')
{
for(j=0;j<cnt;j++)
{
if(map[a[j].x][a[j].y]=='P')
{
q.x=a[j].x;
q.y=a[j].y;
q.val=temp.val;
que.push(q);
map[q.x][q.y]='#';
}
}

}
}
}

}
return ans;
}
int main()
{
int i,j,ans;
while(scanf("%d %d %d",&hang,&lie,&val)!=EOF)
{
cnt=0;
for(i=0;i<hang;i++)
{
scanf("%s",map[i]);
for(j=0;j<lie;j++)
if(map[i][j]=='Y') {s_x=i;s_y=j;}
else if(map[i][j]=='C') {e_x=i;e_y=j;}
else if(map[i][j]=='P') {a[cnt].x=i;a[cnt++].y=j;}
}
ans=BFS();
if(ans!=999999999)
printf("%d\n",ans);
else printf("Damn teoy!\n");
}
return 0;
}

对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别上a[i],然后再将和的结果作为新的候选值入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有和结果作为新的候选值入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值