题目链接: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. Test SRM Level Three: LargestCircle, Brute Force

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3005&rd=5858 思路: 如果直接用Brute F ...

  2. Android锁定屏幕或关闭状态-screen,高速按两次音量向下键来实现拍摄功能(1.1Framework在实现的形式层广播)

    思想的实现:     WindowManagerService循环读取下面的关键信息和分发形式.在PhoneWindowManager.interceptKeyBeforeQueueing方法中进行消 ...

  3. 顺序容器的insert使用方法

    #include <iostream> #include <algorithm> #include <vector> #include <string> ...

  4. Nginx各版本的区别

    Nginx官网提供了三个类型的版本Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版Stable version:最新稳定版,生产环境上建议使用的版 ...

  5. 《数字图像处理原理与实践(MATLAB文本)》书代码Part7

    这篇文章是<数字图像处理原理与实践(MATLAB文本)>一本书的代码系列Part7(由于调整先前宣布订单,请读者注意分页程序,而不仅仅是基于标题数的一系列文章),第一本书特色186经225 ...

  6. OCP-1Z0-051-题目解析-第14题

    14. Using the CUSTOMERS table,  you need to generate a report that shows 50% of each credit        a ...

  7. 综合第一篇文章(带钩Quora)

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDc4MzAyNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  8. 纯 Swift 封装的 SQLite 框架:SQLite.swift

    SQLite.swift 是一个使用纯 Swift 语言封装 SQLite3 的操作框架. 特性: 简单的查询和参数绑定接口 安全.自动类型数据访问 隐式提交和回滚接口 开发者友好的错误处理和调试 文 ...

  9. PHP PDO sqlite ,Unable to Open database file的解决方法

    t.php在网站的根目录. fdy.db在inc文件夹下; t.php中sqlite路径写成相对路径 $db = new PDO('sqlite:inc/fdy.db'); 开始提示 Fatal er ...

  10. docker-gitlab(转)

    Issues Docker is a relatively new project and is active being developed and tested by a thriving com ...