题目链接: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.
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.
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;
}