首页 > 其他分享 >hdu1242 Rescue--BFS

hdu1242 Rescue--BFS

时间:2022-12-06 19:37:32浏览次数:78  
标签:ch -- tt BFS int step pq include hdu1242


原题链接: ​​ http://acm.hdu.edu.cn/showproblem.php?pid=1242​


一:题意

x代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙
路花费一秒,x花费两秒
问到达终点的最少时间
思路:BFS+优先队列


二:AC代码

#define _CRT_SECURE_NO_DEPRECATE 
#define _CRT_SECURE_Cy1_OVERLOAD_STANDARD_NAMES 1

#include<iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;

struct Node
{
int x, y;
int step;
bool operator<(const Node & node) const
{
return node.step < step;
}
};

char ch[205][205];
int dis[205][205];
int dir[4][2] = { 1,0,-1,0,0,1,0,-1 };
int sx, sy;//起点,也就是朋友的位置
int dx, dy;
int n, m;

bool bfs()
{
Node t, tt;
priority_queue<Node> pq;
t.x = sx;
t.y = sy;
t.step = 0;
pq.push(t);
dis[sx][sy] = 0;

while (!pq.empty())
{
t = pq.top();
pq.pop();
if (t.x == dx&&t.y == dy)
{
printf("%d\n", t.step);
return true;
}

for (int i = 0; i < 4; i++)
{
tt = t;
tt.x += dir[i][0];
tt.y += dir[i][1];
if (tt.x < 0 || tt.y < 0 || tt.x >= n || tt.y >= m || ch[tt.x][tt.y] == '#')
continue;

tt.step++;
if (ch[tt.x][tt.y] == 'x')
tt.step++;

if (dis[tt.x][tt.y] >= tt.step)
{
dis[tt.x][tt.y] = tt.step;
pq.push(tt);
}
}
}

return false;
}

int main()
{
while (~scanf("%d%d", &n, &m))
{
for (int i = 0; i < n; i++)
{
getchar();
for (int j = 0; j < m; j++)
{
dis[i][j] = 99999999;
scanf("%c", &ch[i][j]);
if (ch[i][j] == 'a')
{
dx = i;
dy = j;
}
else if (ch[i][j] == 'r')
{
sx = i;
sy = j;
}
}
}

if (!bfs())
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}







标签:ch,--,tt,BFS,int,step,pq,include,hdu1242
From: https://blog.51cto.com/u_11937443/5916548

相关文章

  • 使用Spring Reactor优化推荐流程
    1.背景公司有一个推荐系统Rec,这个系统的主要功能是:向外部系统提供推荐接口根据请求获取推荐策略根据推荐策略完成推荐的召回、过滤、打分、排序阶段Rec作为微服务......
  • hdu1195 Open the Lock--单向BFS & 双向BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1195​​一:题意两个四位数的数字,经过一下三种方式变换,求出变成另一个数字的最小时间。加1;减1;相邻交换其中9+1变......
  • hdu1175 连连看 --DFS/BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1175​​直接上代码,不是很难。#define_CRT_SECURE_NO_DEPRECATE#define_CRT_SECURE_Cy1_OVERLOAD_STANDARD_N......
  • UE4学习笔记23——【动画】Mixamo自动绑骨并导入虚幻4
    P61.Mixamo自动绑骨并导入虚幻4P61需要插件“MixamoAnimationRetargeting”(200多块......)(这节课就简单听听,以后用到了再看)(桥豆麻袋!不用买这个插件,这节课的东西也能......
  • hdu1026 Ignatius and the Princess I --BFS & 记录路径 & 与DFS的比较
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1026​​一:题意一个n*m的矩阵代表一个迷宫,(0,0)是起点,(n-1)(m-1)是终点,每移动一步一秒。迷宫每点意义是:. 该点可以......
  • Angular 表单
    表单中的重要概念1:FormControl:表单控件,封装了表单中的输入,并提供了一些可供操纵的对象2:Validator:验证器,对表单的输入根据自己的需要添加一些限制3:Observer:观察者,......
  • 5.3.1 (2) 函数的单调性(含参函数)
    \({\color{Red}{欢迎到学科网下载资料学习}}\)[【基础过关系列】高二数学同步精品讲义与分层练习(人教A版2019)](https://www.zxxk.com/docpack/2875423.html)\({\col......
  • hdu1010 Tempter of the Bone --DFS & 奇偶剪枝
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1010​​一:题意一个n*m的迷宫,给定一个出发点,一个结束点,一条小狗需要在恰好第k秒走到结束点,如果可以输出YES,不然输......
  • k1s 工具使用教程
    .1.k1s是kubectl辅助工具soeasy,sofast.___/\/\/\(k|1|s)\_/\_/\_/by百里(github.com/yezihack/k1s)soeasy,s......
  • 判断是否是完全二叉树
     对于一棵二叉树,判断是否是一棵完全二叉树。typedefstructNode{intkey;Node*lChild;Node*rChild;}*PNode;boolis(PNode&p){boolflag=false;queue<PN......