bfs问题。

Angel有被关在监狱,她有非常多朋友要去救她。

#表示墙,.表示路,x表示警卫,r表示她的朋友。

因为可能有非常多朋友,可是Angel仅仅有一个,所以搜索起点设为Angel。仅仅要找到一个朋友表示能走出去。

走一格须要1,杀死警卫须要1,假设使用 queue 不能直接加2.

由于会出现这样的情况

4 8
axxxxxxr
........
........
........

假设直接加2的话,答案就不是9.

可是使用 priority_queue 就能够无视这样的情况了。我也要開始习惯使用priority_queue。

queue版本号。

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<vector>
#include<cmath>#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define PI 3.141592654
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,n) for(int i= a;i< n ;i++)
#define debug puts("==fuck==")
#define acfun std::ios::sync_with_stdio(false)#define SIZE 1000+10
using namespace std;int xx[]={0,0,-1,1};
int yy[]={-1,1,0,0};
int n,m;
char g[201][201];struct lx
{int x,y,lv;void init(int xx,int yy,int llv){x=xx,y=yy,lv=llv;}
};lx start,thend;void bfs()
{queue<lx>q;bool vis[201][201];CLR(vis,0);q.push(start);vis[start.x][start.y]=1;while(!q.empty()){lx tmp=q.front();q.pop();
//        printf("%d %d == %d\n",tmp.x,tmp.y,tmp.lv);
//        system("pause");if(g[tmp.x][tmp.y]=='r'){printf("%d\n",tmp.lv);return ;}FOR(k,0,4){int x=tmp.x+xx[k];int y=tmp.y+yy[k];if(x<0||y<0||x>=n||y>=m||g[x][y]=='#'||vis[x][y])continue;lx now;if(g[x][y]=='x'){now.init(tmp.x,tmp.y,tmp.lv+1);g[x][y]='.';}else{now.init(x,y,tmp.lv+1);vis[x][y]=1;}q.push(now);}}puts("Poor ANGEL has to stay in the prison all his life.");
}int main()
{while(~scanf("%d%d",&n,&m)){char str[201];FOR(i,0,n){scanf("%s",str);FOR(j,0,m){g[i][j]=str[j];if(g[i][j]=='a')start.init(i,j,0);}}bfs();}
}

priority_queue 版本号

#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<vector>
#include<cmath>#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define PI 3.141592654
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,n) for(int i= a;i< n ;i++)
#define debug puts("==fuck==")
#define acfun std::ios::sync_with_stdio(false)#define SIZE 200+10
using namespace std;int xx[]={0,0,-1,1};
int yy[]={-1,1,0,0};
int n,m;
char g[SIZE][SIZE];
struct lx
{int x,y,lv;void init(int xx,int yy,int llv){x=xx,y=yy,lv=llv;}friend bool operator< (lx a,lx b){return a.lv>b.lv;}
};
lx start;void bfs()
{priority_queue<lx>q;bool vis[SIZE][SIZE];CLR(vis,0);vis[start.x][start.y]=1;q.push(start);while(!q.empty()){lx tmp=q.top();q.pop();if(g[tmp.x][tmp.y]=='r'){printf("%d\n",tmp.lv);return ;}FOR(k,0,4){int x=tmp.x+xx[k];int y=tmp.y+yy[k];if(x<0||y<0||x>=n||y>=m||vis[x][y]||g[x][y]=='#')continue;lx now;if(g[x][y]=='x')now.init(x,y,tmp.lv+2);elsenow.init(x,y,tmp.lv+1);vis[x][y]=1;q.push(now);}}puts("Poor ANGEL has to stay in the prison all his life.");
}
int main()
{while(~scanf("%d%d",&n,&m)){char str[SIZE];FOR(i,0,n){scanf("%s",str);FOR(j,0,m){g[i][j]=str[j];if(g[i][j]=='a')start.init(i,j,0);}}bfs();}
}

转载于:https://www.cnblogs.com/mengfanrong/p/4388480.html

HDU 1242 Rescue相关推荐

  1. HDU 1242 Rescue BFS+优先队列

    题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=1242 #include <stdio.h> #include <stri ...

  2. (step4.2.3)hdu 1242(Rescue——BFS)

    题目大意:friends用最短的时间去救angel '.'表示通道 '#'表示墙壁 'x'表示guard.走一格要一单位时间,杀死一个guard要一个单位时间. 如果可以救求最短时间,否则按要求输出 ...

  3. *【HDU - 1242 】 Rescue (反向dfs,或bfs)

    题干: Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N ...

  4. 1242 Rescue

    题目详情: Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  5. 1242 Rescue BFS

    #include<iostream> #include<string> #include<string.h> #include<stdio.h> #in ...

  6. BFS HDOJ 1242 Rescue

    题目传送门 题意:从r走到a,遇到x多走一步,问最小走到a的步数 分析:因为r有多个,反过来想从a走到某个r的最小步数,简单的BFS.我对这题有特殊的感情,去年刚来集训队时肉鸽推荐了这题,当时什么都不 ...

  7. HDU - 1242

    原题: 传送门 题意: #的格子不能走:. 的可以走,要1个单位时间:x 的格子也可以走,不过要2个单位时间,求从r到a的最小时间 思路: bfs搜索 注意: 不同于传统的bfs,这次走一个格子的时间 ...

  8. 杭电OJ分类题目(1)

    原题出处:HDOJ Problem Index by Type,http://acm.hdu.edu.cn/typeclass.php 杭电OJ分类题目(1) HDU Introduction HDU ...

  9. hdu1242 Rescue DFS(路径探索题)

    hdu1242 Rescue DFS(路径探索题) 这里我定义的路径探索题指 找某路能够到达目的地,每次走都有方向,由于是探索性的走 之后要后退 那些走过的状态都还原掉 地址:http://acm.h ...

最新文章

  1. Python 字典(Dictionary) copy()方法
  2. IOS 百度地图获取当前屏幕的经纬度
  3. USACO 1.0_Friday the Thirteenth
  4. SAP Spartacus全局配置模块里和layoutSlot相关的配置
  5. 基于B/S架构的故障模型
  6. oracle 图像包,完美简单详细,图形安装Oracle11g(Oracle Linux系统)
  7. Linux 命令(88)—— more 命令
  8. H5U PLC定位控制功能块(EtherCAT总线)
  9. 修补计算机漏洞重启,win7系统出现严重的系统漏洞如何修复
  10. sns.relplot
  11. 企业的病毒,要及时清理
  12. MARKETS AND MARKET LOGIC——The Market‘s Principles (1)
  13. Unity3d shader 教程一 准备
  14. 全球及中国常规救生艇行业研究及十四五规划分析报告
  15. 电容有什么作用?为什么cpu电源引脚都并联一个电容?
  16. vue3:父子组件传值
  17. 简单搭建微服务springCloudNetflix服务(一)
  18. 东软java的笔试_东软java面向对象程序设计笔试题
  19. flash芯片替换SAMSUNG K9K8G08U0E替换K9K8G08U0B
  20. Day 31-35 : 玩转Linux操作系统

热门文章

  1. java struts2 xss_Apache Struts2提供的最新demo中几处XSS(不严谨啊!不严谨啊!)
  2. VS创建第一个程序hello(跨文件)
  3. linux 6.4 安装oracle10g,Red Hat Linux 6.4 安装 Oracle 10g 及问题解决
  4. kafka zookeeper java_简单搭建kafka + zookeeper,附简单Java生产和消费客户端
  5. linux qt创建静态库,QT创建与QT无关的纯C++程序和动态/静态库
  6. python随机森林 交叉验证_随机森林是否需要交叉验证+特征的重要性
  7. python 进度条_6种酷炫Python运行进度条
  8. linux用户管理命令 2
  9. linux安装yum的脚本,lnmp一键安装脚本yum方式快速安装
  10. 淘宝开源Key/Value结构数据存储系统Tair技术剖析