题目连接
http://acm.hdu.edu.cn/showproblem.php?pid=4308
Saving Princess claire_
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
bfs。。
#include<algorithm> #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #include<vector> #include<queue> #include<set> using std::set; using std::pair; using std::swap; using std::queue; using std::multiset; #define pb(e) push_back(e) #define sz(c) (int)(c).size() #define mp(a, b) make_pair(a, b) #define all(c) (c).begin(), (c).end() #define iter(c) decltype((c).begin()) #define cls(arr, val) memset(arr, val, sizeof(arr)) #define cpresent(c, e) (find(all(c), (e)) != (c).end()) #define rep(i, n) for(int i = 0; i < (int)n; i++) #define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i) const int N = 5010; const int INF = 0x3f3f3f3f; struct Node { int x, y, s; Node(int i = 0, int j = 0, int k = 0) :x(i), y(j), s(k) {} }P[N / 10]; char G[N][N]; bool vis[N][N]; int H, W, tot, Sx, Sy, cost; const int dx[] = { 0, 0, -1, 1 }, dy[] = { -1, 1, 0, 0 }; int bfs() { queue<Node> q; q.push(Node(Sx, Sy)); while (!q.empty()) { Node t = q.front(); q.pop(); rep(i, 4) { int x = dx[i] + t.x, y = dy[i] + t.y; if (x < 0 || x >= H || y < 0 || y >= W) continue; if (vis[x][y] || G[x][y] == '#') continue; vis[x][y] = true; if (G[x][y] == 'C') return t.s; if (G[x][y] == '*') q.push(Node(x, y, t.s + 1)); if (G[x][y] == 'P') { rep(i, tot) { Node &k = P[i]; q.push(Node(k.x, k.y, t.s)); vis[k.x][k.y] = true; } } } } return -1; } int main() { #ifdef LOCAL freopen("in.txt", "r", stdin); freopen("out.txt", "w+", stdout); #endif while (~scanf("%d %d %d", &H, &W, &cost)) { tot = 0; rep(i, H) { scanf("%s", G[i]); rep(j, W) { vis[i][j] = false; if (G[i][j] == 'Y') Sx = i, Sy = j; if (G[i][j] == 'P') P[tot++] = Node(i, j); } } int ret = bfs(); if (-1 == ret) puts("Damn teoy!"); else printf("%d ", ret * cost); } return 0; }