首页 > 其他分享 >hdu1180 诡异的楼梯--BFS

hdu1180 诡异的楼梯--BFS

时间:2022-12-06 19:37:54浏览次数:75  
标签:Map -- hdu1180 int step && BFS dir tmp2


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


需要注意这句话:Harry每秒只能停留在'.'或'S'和'T'所标记的格子内.

#define _CRT_SECURE_NO_DEPRECATE 
#define _CRT_SECURE_Cy1_OVERLOAD_STANDARD_NAMES 1

#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;

const int N = 50;
const int INF = 1000000;

char Map[N][N];
bool vis[N][N];
int n, m;

int dir[4][2] = { { 1, 0 },{ -1, 0 },{ 0, 1 },{ 0, -1 } };

struct node
{
int x, y;
int step;
friend bool operator < (const node &a, const node &b)
{
return a.step > b.step;
}
};

node st;

bool check(int x, int y)
{
if (x >= 0 && x < n && y >= 0 && y < m && !vis[x][y] && Map[x][y] != '*')
return true;
else
return false;
}

int bfs(int x, int y)
{
char c;
priority_queue <node> q;
node s_pos;
s_pos.x = x;
s_pos.y = y;
s_pos.step = 0;
vis[s_pos.x][s_pos.y] = 1;
q.push(s_pos);
while (!q.empty())
{
node tmp = q.top();
q.pop();
for (int i = 0; i < 4; i++)
{
node tmp2;
tmp2 = tmp;
tmp2.x += dir[i][0];
tmp2.y += dir[i][1];
tmp2.step += 1;
if (check(tmp2.x, tmp2.y) && (Map[tmp2.x][tmp2.y] == '-' || Map[tmp2.x][tmp2.y] == '|'))
{
if (tmp2.step % 2 == 1)
{
if (Map[tmp2.x][tmp2.y] == '-') c = '|';
else if (Map[tmp2.x][tmp2.y] == '|') c = '-';
}
else c = Map[tmp2.x][tmp2.y];

tmp2.x += dir[i][0];//????
tmp2.y += dir[i][1];

if ((c == '-' && (dir[i][1] == -1 || dir[i][1] == 1)) || (c == '|' && (dir[i][0] == -1 || dir[i][0] == 1)))
tmp2.step += 1;

}
if (check(tmp2.x, tmp2.y))
{
if (Map[tmp2.x][tmp2.y] == 'T') return tmp2.step;
vis[tmp2.x][tmp2.y] = 1;
q.push(tmp2);
}
}
}
return -1;
}

int main()
{
while (~scanf("%d%d", &n, &m))
{
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
cin >> Map[i][j];
if (Map[i][j] == 'S')
{
st.x = i;
st.y = j;
}
}
memset(vis, 0, sizeof(vis));
int ans = bfs(st.x, st.y);
printf("%d\n", ans);
}
return 0;
}




标签:Map,--,hdu1180,int,step,&&,BFS,dir,tmp2
From: https://blog.51cto.com/u_11937443/5916547

相关文章

  • hdu1242 Rescue--BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1242​​一:题意x代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙路花费一秒,x花费两秒问到达终点的最少时间思路:B......
  • 使用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......