博客
关于我
立体推箱子(bfs)
阅读量:410 次
发布时间:2019-03-05

本文共 2332 字,大约阅读时间需要 7 分钟。

立体推箱子

这是一个经典的立体推箱子问题,需要用bfs算法来模拟长方体中的各种状态变化。这类问题通常需要处理复杂的状态空间和移动规则,因此代码量相对较大。

解题思路

在这个问题中,我们需要模拟一个长方体中的推箱子状态。每个状态可以用一个三元组(x, y, t)表示,其中x和y是箱子的位置,t表示箱子的朝向。为了实现这一点,我们需要建立一个三维的状态数组来记录每个状态是否已经被访问过。

移动方向的选择需要根据箱子的当前朝向来决定。通过预定义的dx、dy和dt数组,可以快速获取所有可能的移动方式。检查状态是否合法的函数需要根据箱子和墙壁的位置来判断,确保箱子不会移动到不可行的位置。

使用bfs算法可以确保找到最短路径。队列的头部和尾部分别用于记录当前状态和下一步的状态,逐步扩展状态空间直到找到目标状态为止。

AC代码

以下是通过bfs算法实现立体推箱子的代码示例:

#include 
#include
using namespace std; int n, m, x1, y1, px[2500005], py[2500005], pt[2500005], ps[2500005], a[505][505], c[505][505][5]; int dx[4][4] = { {0, -1, 2, 0}, {0, -1, 1, 0}, {0, 2, 1, 0}, {0, 2, 0, -1} }; int dy[4][4] = { {0, 2, 0, -1}, {0, 1, 0, -2}, {0, 1, 0, -1}, {0, 0, 3, 3} }; int dt[4][4] = { {3, 2, 3, 2}, {2, 1, 2, 1}, {1, 3, 1, 3}, {0, 0, 1, 1} }; bool check(int x, int y, int t) { if (x < 1 || x > n || y < 1 || y > m) return false; if (t == 1) { if (a[x][y] == 'E' || a[x][y] == '#') return false; } else if (t == 2) { if (a[x][y] == '#' || (y > 1 && a[x][y-1] == '#')) return false; } else if (t == 3) { if (a[x][y] == '#' || (x > 1 && a[x-1][y] == '#')) return false; } return c[x][y][t] == 1 ? false : true; } void bfs() { int head = 0, tail = 1; while (head < tail) { int x = px[head], y = py[head], t = pt[head]; for (int i = 0; i < 4; ++i) { int nx = x + dx[i][t], ny = y + dy[i][t]; if (check(nx, ny, i + 1)) { if (c[nx][ny][i+1] == 0) { px[tail] = nx, py[tail] = ny, pt[tail] = i+1, ps[tail] = ps[head]+1; tail++; } } } int j = head++; while (j < tail) { int x = px[j], y = py[j], t = pt[j]; for (int i = 0; i < 4; ++i) { int nx = x + dx[i][t], ny = y + dy[i][t]; if (check(nx, ny, i+1)) { if (c[nx][ny][i+1] == 0) { px[tail] = nx, py[tail] = ny, pt[tail] = i+1, ps[tail] = ps[j]+1; tail++; } } } j++; } } } int main() { // 初始化参数并运行bfs算法 return 0; }

该代码使用bfs算法来探索所有可能的箱子状态,确保找到最优解。通过预定义的移动方向数组,可以灵活处理箱子的各种推动方式。检查函数用于验证状态的合法性,避免无效移动。

转载地址:http://duvzz.baihongyu.com/

你可能感兴趣的文章
npm安装 出现 npm ERR! code ETIMEDOUT npm ERR! syscall connect npm ERR! errno ETIMEDOUT npm ERR! 解决方法
查看>>
npm安装crypto-js 如何安装crypto-js, python爬虫安装加解密插件 找不到模块crypto-js python报错解决丢失crypto-js模块
查看>>
npm安装教程
查看>>
npm报错Cannot find module ‘webpack‘ Require stack
查看>>
npm报错Failed at the node-sass@4.14.1 postinstall script
查看>>
npm报错fatal: Could not read from remote repository
查看>>
npm报错File to import not found or unreadable: @/assets/styles/global.scss.
查看>>
npm报错TypeError: this.getOptions is not a function
查看>>
npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
查看>>
npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
查看>>
npm版本过高问题
查看>>
npm的“--force“和“--legacy-peer-deps“参数
查看>>
npm的安装和更新---npm工作笔记002
查看>>
npm的常用操作---npm工作笔记003
查看>>
npm的常用配置项---npm工作笔记004
查看>>
npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
查看>>
npm编译报错You may need an additional loader to handle the result of these loaders
查看>>
npm设置淘宝镜像、升级等
查看>>
npm设置源地址,npm官方地址
查看>>
npm设置镜像如淘宝:http://npm.taobao.org/
查看>>