首页 > 其他分享 >hdu1254 推箱子--BFS

hdu1254 推箱子--BFS

时间:2022-12-06 19:38:21浏览次数:82  
标签:ps -- mmap tt BFS int flag pd hdu1254


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


一:分析

分两步,一是箱子走到终点,二是人得走到箱子的前一个位置。

hdu1254 推箱子--BFS_BFS

先是bfs_box在 t 点找到一个可行点tt,进而用bfs_people判断ps能否到达pd点。


二:AC代码

#define _CRT_SECURE_NO_DEPRECATE 
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1

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

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

struct Node
{
int x, y;
int step;
int mmap[10][10];
bool cheak()
{
if (x >= 0 && x < n&&y >= 0 && y < m)
return true;
return false;
}
};

Node pd;//人的目标
Node ss;
Node ps;//人的起点

/* 搜索人是否能到达指定位置 */
bool bfs_people(Node p)
{
queue<Node> Q;
ps = p;
bool flag[10][10];
memset(flag, 0, sizeof(flag));

for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if (ps.mmap[i][j] == 4)//找到人的起点
{
ps.x = i;
ps.y = j;
}

if (ps.x == pd.x&&ps.y == pd.y)//pd是人的目标位置
return true;

Q.push(ps);
flag[ps.x][ps.y] = 1;

while (!Q.empty())
{
Node t = Q.front();
Q.pop();

for (int i = 0; i < 4; i++)
{
Node tt = t;
tt.x += dir[i][0];
tt.y += dir[i][1];

if (tt.cheak() && flag[tt.x][tt.y] == 0 && p.mmap[tt.x][tt.y] != 1 && p.mmap[tt.x][tt.y] != 2)
{
flag[tt.x][tt.y] = 1;
if (tt.x == pd.x&&tt.y == pd.y)
return true;
Q.push(tt);
}
}
}

return 0;
}

/* 搜索箱子 */
int bfs_box()
{
int flag[10][10][4];
queue<Node> Q;
Node t, tt;

Q.push(ss);
memset(flag, 0, sizeof(flag));

while (!Q.empty())
{
t = Q.front();
Q.pop();

for (int i = 0; i < 4; i++)
{
tt = t;
tt.x += dir[i][0];
tt.y += dir[i][1];
tt.step++;

if (tt.cheak() && ss.mmap[tt.x][tt.y] != 1 && flag[tt.x][tt.y][i] == false)
{
pd.x = t.x - dir[i][0];//bfs_pepple()的目标位置
pd.y = t.y - dir[i][1];
if (pd.cheak() == 0)
continue;

if (bfs_people(tt))
{
//更新地图,箱子和人的位置
swap(tt.mmap[tt.x][tt.y], tt.mmap[t.x][t.y]);
swap(tt.mmap[pd.x][pd.y], tt.mmap[ps.x][ps.y]);
flag[tt.x][tt.y][i] = 1;
if (ss.mmap[tt.x][tt.y] == 3)
return tt.step;
Q.push(tt);
}
}
}
}

return -1;
}

int main()
{
scanf("%d", &t);
while (t--)
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
scanf("%d", &ss.mmap[i][j]);
if (ss.mmap[i][j] == 2)
{
ss.x = i;
ss.y = j;
ss.step = 0;
}
}
}

printf("%d\n", bfs_box());
}

return 0;
}









标签:ps,--,mmap,tt,BFS,int,flag,pd,hdu1254
From: https://blog.51cto.com/u_11937443/5916543

相关文章

  • hdu1240 Asteroids!--DFS & BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1240​​一:题意三维空间,中o表示可以走,x表示不能走,给出行走的起始点和目的点的坐标,问最少多少步可以从起点到达目......
  • hdu1180 诡异的楼梯--BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1180​​需要注意这句话:Harry每秒只能停留在'.'或'S'和'T'所标记的格子内.#define_CRT_SECURE_NO_DEPRECATE#def......
  • 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......