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 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...
随机推荐
- Cocos2d-x实现简单的翻牌效果
触发器互联网影响找了很多.有自己的点重写一个复杂的sprite类来实现.简单的操作来对引擎的使用CCOrbitCamera实现,但是,也存在一些问题,后变反了. 我在用的仅仅是一个简单的翻牌效果,点击 ...
- SendMail如何签名
MailAddress类有两个参数 第1个参数:发送者的邮箱 第2个参数:发送者的签名 示例: MailMessage message = new MailMessage();message.From ...
- 使用 WPF 创建单实例应用程序
一个简单的例子就是大家在使用很多应用程序,例如在使用Microsoft Word 时会遇到一种情况,不管你打开多少个文档,系统一次只能加载一个winword.exe 实例.当打开新文档时,文档在新窗口 ...
- CoreGraphics QuartzCore CGContextTranslateCTM 说明
CoreGraphics.h 一些经常使用旋转常量 #define M_E 2.71828182845904523536028747135266250 e #define M_LOG2E 1.442 ...
- V微软S2015下载:开展Win10/Linux/iOS多平台软件
微软VS2015下载:开展Win10/Linux/iOS多平台软件 资源:IT之家作者:子非 责任编辑:子非 11月13日消息,微软刚刚宣布了 Visual Studio 2015 ...
- Directx11学习笔记【五】 基本的数学知识----向量篇
本文参考dx11龙书 Chapter1 vector algebra(向量代数) 要想学好游戏编程,扎实的数学知识是尤为重要的,下面将对dx11龙书中有关向量的数学知识做一下总结. 在数学中,几何向量 ...
- 为什么MVC不是一种设计模式(转)
MVC(Model-View-Controller)是处理界面应用程序时常用的解决方案,构成了表示层. MVC通过分离模型.视图.控制器在应用程序中的角色,实现界面和业务逻辑的解耦.Model(是OO ...
- 设计模式Adapter模式的五分钟
五分钟一个设计模式.来形容叙述的设计模式的最简单方法.看到许多其他设计模式,请点击五分钟一个设计模式系列 http://blog.csdn.net/daguanjia11/article/catego ...
- 【Shell剧本练习】得出的结论是当前用户
推断是否当前用户root.假设是暗示root用户,假设而不是提示对于普通用户 #!/bin/bash #title: testus.sh #author: orangleliu #date: 2014 ...
- 无显示仍然发挥树莓派——VNCserver设定
谁说没有显示器就不能玩树莓派的图形界面了.不要忘了VNCserver哦! VNC(Virtual Network Computing)属于一种网络显示系统,也就是说它能将完整的窗体界面通过网络传输到还 ...