题目链接: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)的更多相关文章

  1. hdu 4531 bfs(略难)

    题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...

  2. HDU 1242 Rescue(BFS),ZOJ 1649

    题目链接 ZOJ链接 Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The ...

  3. HDU 1242 Rescue(BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by t ...

  4. hdu 1242 Rescue (BFS)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. ZOJ-1649 Rescue BFS (HDU 1242)

    看题传送门: ZOJ http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1649 HDU http://acm.hdu.edu. ...

  6. Saving Princess claire_(hdu 4308 bfs模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Jav ...

  7. HDU 2822 (BFS+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2822 题目大意:X消耗0,.消耗1, 求起点到终点最短消耗 解题思路: 每层BFS的结点,优先级不同 ...

  8. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  9. HDU 2531 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

随机推荐

  1. svn加入新的文件夹

    方法一: 1.在远程server上生成新的文件夹 svn mkdir http://svn.xxx.com/svn/mobile/strategy/assistant/branches/talk -m ...

  2. ps设计资料整理

    零基础学会建立一个简单化妆品网站—前台设计篇1[PS画草图] http://xiebiji.com/2008/09/huazhuang4/?wptheme=Plainscape&ie=1 PS ...

  3. NET WEB

    .NET WEB程序员需要掌握的技能 2015-12-28 08:50 by 敏捷的水, 3997 阅读, 66 评论, 收藏, 编辑 本来这个是我给我们公司入职的新人做一个参考,由于 @张善友 老师 ...

  4. sql基础之DDL(Data Definition Languages)

    好久没写SQL语句了,复习一下. DDL数据定义语言,DDL定义了不同的数据段.数据库.表.列.索引等数据库对象的定义.经常使用的DDL语句包含create.drop.alter等等. 登录数据:my ...

  5. hdu1506——Largest Rectangle in a Histogram

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  6. 利用Eclipse中的Maven构建Web项目(三)

    利用Eclipse中的Maven构建Web项目 1.将Maven Project转换成动态Web项目,鼠标右键项目,输入"Project Facets" 2.依据Dynamic W ...

  7. 使用axis公布weblogic(一个)

    1.在MyEclipse创建一个新的Web Project.新类,声明函数名称.参数,为了不写功能实体. 2.将java2wsdl.bat将文件复制到classes目录,据内部参数更改自己的实际情况 ...

  8. Meld Diff for windows 安装和配置

    Meld Diff for windows 安装和配置 假设你在ubuntu 正在开发中, meld diff 此工具你肯定不会感到陌生. 而且很容易使用. 在网上看 meld for Windows ...

  9. WPF六个控制概述

    在线演示:http://v.youku.com/v_show/id_XNzA0NjU1Mjk2.html 清晰版视频+代码下载:http://115.com/lb/5lbcftnrfo9s 一.简单介 ...

  10. 余弦信号DFT频谱分析(继续)

    以前谈到序列的实际长度可以通过零填充方法加入,使得最终增加N添加表观分辨率. 但它并没有解决泄漏频率的问题. 根本原因在于泄漏窗口选择的频率. 由于矩形窗突然被切断,频谱旁瓣相对幅度过大,造成泄漏分量 ...