博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 4308 Saving Princess claire_(bfs,4级)
阅读量:4455 次
发布时间:2019-06-07

本文共 3420 字,大约阅读时间需要 11 分钟。

Saving Princess claire_

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1497    Accepted Submission(s): 559
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
 
Recommend
zhuyuanchen520

思路:BFS,搜y->c结束,y->p 就将所有的P压入栈。

容易爆内存,最好不要自己写循环队列

#include
#include
#include
#include
#include
using namespace std;const int mm=5005;const int oo=1e9;const int dx[]={1,-1,0,0};const int dy[]={0,0,1,-1};char s[mm][mm];bool vis[mm][mm];int r,c,cost;int ans;class node{public:int x,y,step;}ss,ee,f[mm];int pos;queue
q;void bfs(){ while(!q.empty())q.pop(); node z,t;q.push(ss); memset(vis,0,sizeof(vis)); vis[ss.x][ss.y]=1; while(!q.empty()) { z=q.front();q.pop(); for(int i=0;i<4;++i) { t.x=z.x+dx[i];t.y=z.y+dy[i];t.step=z.step; if(t.x<0||t.x>=r||t.y<0||t.y>=c||vis[t.x][t.y]||s[t.x][t.y]=='#')continue; vis[t.x][t.y]=1; if(s[t.x][t.y]=='C'){ans=t.step;return;} if(s[t.x][t.y]=='P') { for(int j=0;j

转载于:https://www.cnblogs.com/nealgavin/archive/2013/05/22/3205922.html

你可能感兴趣的文章
SharePoint文档库,如何在新窗口打开中的文件
查看>>
Xmind日常操作
查看>>
java: -source 1.6 中不支持 switch 中存在字符串
查看>>
Card Stacking 队列模拟
查看>>
抽象类和接口的关系与区别哦
查看>>
【C语言】C语言函数
查看>>
为什么用到混合支付?
查看>>
4.STL六大组件
查看>>
java学习之—栈
查看>>
1.5 重点
查看>>
子序列的按位或 Bitwise ORs of Subarrays
查看>>
IN语句改写EXISTS
查看>>
C#-WinForm-用户控件如何获取父级窗体
查看>>
STL_vector
查看>>
Dev中GridView——背景颜色改变
查看>>
socket编程2
查看>>
web开发中的MVC框架与django框架的MTV模式
查看>>
django添加导包路径
查看>>
java基础知识—变量、数据类型和运算符
查看>>
hadoop队列管理(指定queue跑程序)
查看>>