hdu----(4308)Saving Princess claire_(搜索)
Saving Princess claire_
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2354 Accepted Submission(s): 843
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
Author
BUPT
Source
2012 Multi-University Training Contest 1
拥有传送们的搜索.....wa了一下午....(/ □ )
终于搞成AC了....
代码:
1 #include<cstring>
2 #include<cstdio>
3 #include<queue>
4 #include<vector>
5 using namespace std;
6
7 const int maxn=5003;
8 int cc,rr,cost;
9 char str[maxn][maxn];
10 struct node {
11 int x,y;
12 int val;
13 };
14 vector <node> pot;
15
16 int dir[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
17 node st,en;
18 queue<node>seek;
19 node tem,qq;
20 vector<node>::iterator it;
21 int main(){
22
23 //freopen("test.in","r",stdin);
24 while(scanf("%d%d%d",&rr,&cc,&cost)!=EOF){
25
26 pot.clear();
27 vector< vector<node> > map(rr+10);
28 for(int i=0;i<rr+10;i++)
29 map[i].resize(cc+10);
30 for(int i=0;i<rr;i++){
31 scanf("%s",str[i]);
32 for(int j=0;j<cc;j++){
33 map[i][j]=(node){i,j,0};
34 if(str[i][j]=='P'){
35 pot.push_back((node){i,j,0});
36 }
37 if(str[i][j]=='Y')
38 st=(node){i,j,0};
39 if(str[i][j]=='C'){
40 en=(node){i,j,-1};
41 map[i][j]=(node){i,j,-1};
42 }
43 }
44 }
45 //bfs();
46 seek.push(st);
47 while(!seek.empty()){
48 tem=seek.front();
49 seek.pop();
50 for(int i=0;i<4;i++){
51 qq=(node){tem.x+dir[i][0],tem.y+dir[i][1],0};
52 if(qq.x>=0&&qq.x<rr&&qq.y>=0&&qq.y<cc&&str[qq.x][qq.y]!='#'){
53 if(str[qq.x][qq.y]!='C'){
54 if(str[qq.x][qq.y]=='P'){
55 str[qq.x][qq.y]='#';
56 if(map[qq.x][qq.y].val==0||map[qq.x][qq.y].val>map[tem.x][tem.y].val)
57 map[qq.x][qq.y].val=map[tem.x][tem.y].val;
58 if(pot.size()!=0){
59 for(it=pot.begin();it<pot.end();it++)
60 if((*it).x==qq.x&&(*it).y==qq.y) break;
61 pot.erase(it);
62 }
63 if(pot.size()!=0){
64 for(it=pot.begin();it<pot.end();it++){
65 map[(*it).x][(*it).y].val=map[qq.x][qq.y].val;
66 seek.push((map[(*it).x][(*it).y]));
67 str[(*it).x][(*it).y]='#';
68 }
69 }else seek.push(map[qq.x][qq.y]);
70
71 }
72 else if(map[qq.x][qq.y].val==0||map[qq.x][qq.y].val>tem.val+cost){
73 map[qq.x][qq.y].val=tem.val+cost;
74 seek.push(map[qq.x][qq.y]);
75 }
76 }
77 else
78 if(map[qq.x][qq.y].val==-1||map[qq.x][qq.y].val>tem.val)
79 {
80 map[qq.x][qq.y].val=tem.val;
81 }
82 }
83 }
84 }
85 if(map[en.x][en.y].val==-1)
86 printf("Damn teoy!n");
87 else
88 printf("%dn",map[en.x][en.y].val);
89 }
90 return 0;
91 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- redis学习(二十三)
- Vue 轻量级后台管理系统基础模板
- R语言对混合分布中的不可观测与可观测异质性因子分析
- JavaScript同步、异步及事件循环
- Node.js开发人员都应该知道的12个有用的包
- 欧拉函数的几条重要性质
- 迷宫问题的简单栈实现
- xmuC语言程序实践week 3 大作业
- xmuC语言程序实践week 4 大作业
- R语言预测人口死亡率:用李·卡特(Lee-Carter)模型、非线性模型进行平滑估计
- 前端的发展历程
- R语言蒙特卡洛计算和快速傅立叶变换计算矩生成函数
- Visual Studio 中万能头文件编译不了的解决方案
- Difference in two ways of using lower_bound [C++]std::set::lower_bound与std::lower_bound
- 迷你版Vue--学习如何造一个Vue轮子