hdu Rescue (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242
简单优先队列搜索,自己好久不敲,,,,,手残啊,,,,orz
代码:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <ctype.h>
#include <iomanip>
#include <queue>
#include <stdlib.h>
using namespace std; int N,M,ans;
char mp[][];
int vis[][]; int dx[]={,,-,};
int dy[]={-,,,}; int sx,sy;
int ex,ey; struct Node
{
int x,y;
int step;
}; bool operator<(Node a,Node b)//定义结构体类型的优先队列的优先级,从小到大
{
return a.step>b.step;
} void getMap(int n,int m)
{
for(int i=;i<n;i++)
for(int j=;j<m;j++)
{
cin>>mp[i][j];
if(mp[i][j]=='r')
{
sx=i;
sy=j;
}
if(mp[i][j]=='a')
{
ex=i;
ey=j;
}
}
} bool pd(int x,int y)
{
if(x>=&&x<N&&y>=&&y<M&&!vis[x][y]&&mp[x][y]!='#')
return true;
return false;
} int bfs(int x,int y)
{
memset(vis,,sizeof(vis));
priority_queue<Node>q;
Node a,b;
a.x=x,a.y=y,a.step=;
q.push(a);
while(!q.empty()){
b=q.top();
q.pop();
for(int i=; i<; i++){
int px=b.x+dx[i];
int py=b.y+dy[i];
if(pd(px,py)){
vis[px][py]=;
a.x=px;
a.y=py;
if(mp[px][py]=='x'){
a.step=b.step+;
q.push(a);
}
else
{
a.step=b.step+;
q.push(a);
if(px==ex && py==ey)
return a.step;
}
}
}
}
return -;
} int main()
{ while(cin>>N>>M){
getMap(N,M);
int ans=bfs(sx,sy);
if(ans==-)
printf("Poor ANGEL has to stay in the prison all his life.\n");
else
printf("%d\n",ans);
}
}
优先队列搜索:
///优先队列是默认int从大到小priority_queue<int>q1,也可以定义为从小到大priority_queue<int,vector<int>,greater<int> >q2;
也可以自定义优先级,重载< #include <iostream>
#include <functional>
#include <queue>
using namespace std; ///自定义优先级,两种写法,按照优先级从大到小的顺序
/*
struct node
{
friend bool operator<(node n1,node n2)
{
return n1.priority<n2.priority;
}
int priority;
int value;
};*/ struct node
{
int priority;
int value;
};
bool operator<(node a,node b)
{
return a.priority<b.priority;
} int main()
{
const int len=;
int i;
int a[len]={,,,,};
//优先队列中从大到小输出
priority_queue<int>q1;
for(i=;i<len;i++)
q1.push(a[i]);
for(int i=;i<len;i++)
{
cout<<q1.top();
q1.pop();
}
cout<<endl;
//优先队列中从小到大输出 /**
priority_queue<int,vector<int>,greater<int> >q2;
for(i=0;i<len;i++)
q2.push(a[i]);
for(i=0;i<len;i++)
{
cout<<q2.top();
q2.pop();
}*/ priority_queue<int,vector<int>,greater<int> >q;
for(int i=;i<len;i++)
q.push(a[i]);
while(!q.empty())
{
cout<<q.top();
q.pop();
}
cout<<endl;
//按照某个优先级输出,该代码中为priority值大的先输出
priority_queue<node>q3;
node b[len];
b[].priority=;b[].value=;
b[].priority=;b[].value=;
b[].priority=;b[].value=;
b[].priority=;b[].value=;
b[].priority=;b[].value=;
for(i=;i<len;i++)
q3.push(b[i]);
cout<<"优先级"<<'\t'<<"值"<<endl;
for(i=;i<len;i++)
{
cout<<q3.top().priority<<'\t'<<q3.top().value<<endl;
q3.pop();
}
return ;
}
可见链接:http://blog.csdn.net/sr_19930829/article/details/42706469
运行:
96532
23569
优先级 值
9 5
8 2
6 1
2 3
1 4
hdu Rescue (bfs)的更多相关文章
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- HDU 1242 Rescue(BFS),ZOJ 1649
题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...
- HDU 1242 Rescue(BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...
- hdu 1242 Rescue (BFS)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- ZOJ-1649 Rescue BFS (HDU 1242)
看题传送门: ZOJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 HDU http://acm.hdu.edu. ...
- Saving Princess claire_(hdu 4308 bfs模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Jav ...
- HDU 2822 (BFS+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...
- HDU 1180 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...
- HDU 2531 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...
随机推荐
- 下的生产环境was重新启动不同意,怎么做?
前一段时间上线.遇到一个jndi问题,它是如何是个问题?它是在测试环境中的原始没有问题,在生产环境中,您无法连接生产数据库,然后发现问题,那是,ibm工具生成在测试环境中自己主动的连接jndi资源文件 ...
- Ubuntu下搭建ASP.NET 5
在Ubuntu下搭建ASP.NET 5开发环境 0x00 写在前面的废话 年底这段时间实在太忙了,各种事情都凑在这个时候,没时间去学习自己感兴趣的东西,所以博客也好就没写了.最近工作上有个小功能要 ...
- Android Java 与 C++ 恒调用,路径、文件名、延长的最大长度
/**************************************************************************** ********************* ...
- 理解JavaScript的闭包
在JS这块,免不了被问什么是闭包. 从一个常见的循环问题说起. 有一个ul列表, 里面有5个li标签,我希望点击每个li标签的时候,弹出每个li标签对应的索引值(第一个弹出0,第二个弹出1...). ...
- Reactive ExtensionsLINQ和Rx简单介绍
LINQ和Rx简单介绍 相信大家都用过Language Integrated Query (LINQ),他是一种强大的工具能够从集合中提取数据.Reactive Extensions(Rx)是对LIN ...
- MSXML2;System.ServiceModel.Configuration;对应dll的添加方法
- Server SAN:弄潮儿云计算时代
最初发表于<程序猿>2014年7每月一次. 4月30日本.Redhat公布1.71十亿收购Ceph开发商Inktank公司,加上之前2011年10月1.36十亿收购Gluster,Redh ...
- [创意标题] spoj 11354 Amusing numbers
意甲冠军: 给k(1<=k<=10^15),先询问k 大只包含数字5和6的数目是多少 实例 1那是,5 ,3那是,55 .4那是,56 思考: 首先,我们可以找到.有许多2这是头号,有两个 ...
- 初学者应学会如何加快seo
新手学习如何加快seo 介绍: 应该如何初学者学习SEO,前弯路.真正的高手SEO知识. 作为一个新人,学习如何加快seo知识吧? 多人天天都在学习seo知识.看别人的文章.看多了就感觉 ...
- 每天的学习经验:SharePoint 2013 定义自己添加的产品清单。Callout菜单项、文档关注、SharePoint服务机端对象模型查询
前言: 前一段时间一直都比較忙.没有什么时间进行总结,刚好节前项目上线.同一时候趁着放假能够好好的对之前遇到的一些问题进行总结. 主要内容有使用SharePoint服务端对象模型进行查询.为Share ...