hdu4308 Saving Princess claire_

本文介绍了一道名为“Saving Princess Claire”的算法竞赛题目,该题要求寻找从起点到终点的最小花费路径,考虑了包括传送点在内的多种地图元素。文章提供了详细的解题思路及C++实现代码。

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4308


Saving Princess claire_

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2189    Accepted Submission(s): 768


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

大致题意:给你一个r行c列的地图,地图的元素有Y起点,C终点,#不可走点,P隧道口,*可走点

要求从Y到C花费最少(如果能到达),每经过*花费加cost, 遇到P可以到达其他P,没有花费。


题解:bfs,但并不是找到终点就是最短花费,因为P的存在,使答案存在多种,所以要遍历所有情况。

但是题目的数据有点问题,只要找到终点就可以了。

对于P点要进行特殊处理,就是要保存所有P点的位置,当首次遇到P点时,将其他的P点加入队列

(题目:可以从P到其他的P),这样知道队列空,最后判断min是不是初始的值。


代码:

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

struct Node
{
	int x, y;
	int step;
};
struct Port
{
	int x, y;
};
Port pos[5001];
char d[4][2] = {
	{1,0},{-1,0},{0,1}, {0, -1}
};
int r, c, w, sx, sy, ex, ey, pcnt;
char map[5001][5001];
char vis[5001][5001];


void bfs()
{
	memset(vis, 0, sizeof(vis));
	int nx, ny, min = 0x7f7f7f7f;	// 初始min
	Node cur,node;
	cur.x = sx;
	cur.y = sy;
	cur.step = 0;
	queue<Node> q;
	q.push(cur);
	vis[sy][sx] = 1;
	while (!q.empty())
	{
		cur = q.front();
		q.pop();

		for (int i=0; i<4; ++i)
		{
			nx = cur.x + d[i][0];
			ny = cur.y + d[i][1];
			if (nx>=0 && nx<c && ny>=0 && ny<r && !vis[ny][nx])
			{
				if (map[ny][nx] == '*')		// 花费
				{
					vis[ny][nx] = 1;
					node.x = nx;
					node.y = ny;
					node.step = cur.step + w;
					q.push(node);
				}
				else if (map[ny][nx] == 'C')
				{
					if (min > cur.step)
						min = cur.step;
				}
				else if (map[ny][nx] == 'P')	// P加入其他P到队列
				{
					vis[ny][nx] = 1;
					for (int j=0; j<pcnt; ++j)
					{
						if (!vis[pos[j].y][pos[j].x])
						{
							node.x = pos[j].x;
							node.y = pos[j].y;
							node.step = cur.step;
							q.push(node);
							vis[node.y][node.x] = 1;
						}
					}
				}

			}
		}
	}
	if (min == 0x7f7f7f7f)
	{
		puts("Damn teoy!");
	}
	else
		printf("%d\n", min);
}



int main()
{
	while (~scanf("%d%d%d", &r, &c, &w))
	{
		pcnt = 0;
		for (int i=0; i<r; ++i)
		{
			scanf("%s", map[i]);
			for (int j=0; j<c; ++j)
			{
				if (map[i][j] == 'Y')		// 保存起点
				{
					sx = j;
					sy = i;
				}
				else if (map[i][j] == 'C')	// 保存终点
				{
					ex = j;
					ey = i;
				}
				else if (map[i][j] == 'P')	// 保存P点
				{
					pos[pcnt].x = j;
					pos[pcnt++].y = i;
				}
			}
		}

		bfs();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值