题目连接

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\n", ret * cost);
}
return 0;
}

最新文章

  1. Linux下动态库查找路径的问题
  2. sql cross join table
  3. 项目名 的在JSP或JAVA中的另类写法
  4. oracle client ORA-12541: TNS: 无监听程序
  5. hdu 3111 DLX解数独
  6. Httpclient 和jsoup结和提取网页内容(某客学院视频链接)
  7. Largest product in a series
  8. Unity 关于属性的get/set
  9. Day1-while and for/break and continue
  10. vue2购物车ch1-(安装依赖、简单配置、 axios获取api的模拟数据)
  11. linux_inode和block
  12. 学习pthreads,多线程的创建和终止
  13. STL算法设计理念 - 二元函数,二元谓词以及在set中的应用
  14. 怎么单独为ionic2应用的某一组件设置两个平台一致的样式
  15. Codeforces1023F Mobile Phone Network 【并查集】【最小生成树】
  16. go的net/rpc用法
  17. php5.4使用dblib扩展,连接sqlserver中文乱码问题
  18. MS DSVM、DLVM
  19. dephi(pascal)中修改Label字体的样式(加粗,斜体,下划线)
  20. NODE-环境变量的配置(踩坑总结)

热门文章

  1. Robotlegs2 学习笔记 -- SwiftSuspenders 2.x (1)
  2. c语言将2进制数转化为10进制数(栈的初始化,进栈,出栈)
  3. 洛谷P1827 美国血统 American Heritage
  4. js基础笔记
  5. solr5.5教程-schema.xml部分配置
  6. if [-f build/core/envsetup.mk -a -f Makefile ]; then
  7. kbengine mmo源码(完整服务端源码+资源+完整客户端源码)
  8. 当局部变量遇上全局变量——extern及花括号用法举例
  9. 自己实现的一款在线Javascript正则表达式测试器——JRE-Parser
  10. ASPX页面包含inc文件、用户控件、普通html文件