[NOIP模拟]BOX(推箱子)-BFS
来源:互联网 发布:淘宝拆车喇叭 编辑:程序博客网 时间:2024/05/20 12:20
[NOIP模拟] BOX(推箱子) - BFS
题目背景
SOURCE:NOIP2015-SHY-6
题目描述
有个桌子长 R 宽 C ,被分为 R * C 个小方格。其中,一些方格上有箱子,一些方格上有按钮,一些方格上有障碍物,一些方格上是空地。现在有个任务,需要把所有箱子推到这些按钮上面。箱子有个特征,只能推不能搬不能拉。现在需要用最少的步数把所有箱子推到按钮上。
当然,箱子和人都只能以格子为单位移动,不存在一部分在格内一部分在格外的情况;只能向四个方向推,箱子和推箱子的队员都不能穿越障碍物。推动的定义是,人的前进方向被箱子挡住,且箱子在顺着前进方向的下一格不是箱子或者障碍物,那么就可以推着箱子和箱子一起前进一步。
输入格式
输入第一行有两个整数 R,C(4 <= R, C <= 7),代表桌子的长和宽,接下来一共有 R 行,每行有 C 个字符。
字符的含义:
0:代表空地
1:代表障碍物
2:代表箱子
3:代表按钮
4:人所在的初始地方
输入数据保证障碍物环绕整个地图,箱子数目跟按钮数目相同,箱子最少有一个,不会超过 3 个,队员肯定只有一个。不用考虑箱子初始就按着按钮的情况。保证有解。
输出格式
输出只有一行,为解决机关的最小步数。
样例数据 1
输入
4 5
11111
14231
10231
11111
输出
4
备注
[样例说明]
在样例中,最快的方法之一是先向东把第一个箱子推到按钮上,然后向西走回原处,之后向南一步,再向东推第二个箱子到按钮上。至此任务完成,共使用了 4 步。[数据范围]
对 30% 的输入数据 :箱子只有一个。
对 100% 的输入数据 :4 <= R, C <= 7。
题解 :
首先我对这道题没有想法, 原本我以为我数据结构最烂, 现在我终于发现我的搜索最烂。
这道题是到搜索题, 简单来说就是推箱子, 对于推箱子 HDU 1254 是一道入门题, 对于这道题有很多的方法可以做, 这里我写一下 BFS, 我们记录 f[pos][box1][box2][box3], 表示人在 pos 位置, 箱子一在 box1,箱子二在 box2, 箱子三在 box3 的情况的答案, 对于每一种状态都可以转到其他合法的点上, 将所有的情况记录在队列。
pos 实际上是编号这样就不用记录 x, y, 就不用多记几维, 写起来舒服一点。得到编号 :
inline int pos(int x, int y) { return (x - 1) * m + y;}
得到 x 值 :
inline int gety(int x) { return x % m;}
得到 y 值 :
inline int getx(int x) { int now = gety(x); return (x - now) / m + 1;}
记录情况放在队列里 :
inline void insert(int id, int x, int y, int z, int w) { c[id][0] = x; c[id][1] = y; c[id][2] = z; c[id][3] = w;}
访问队列里的元素 :
int head = 1, tail = 1; for(; head <= tail; ++head) { int ren = c[head][0], box1 = c[head][1], box2 = c[head][2], box3 = c[head][3]; int xren = getx(ren), yren = gety(ren), x1 = getx(box1), y1 = gety(box1), x2 = getx(box2), y2 = gety(box2), x3 = getx(box3), y3 = gety(box3); }
每次在 BFS 时只考虑两种状态 :
1. 将要访问点没有障碍, 且不再任何一个箱子上, 直接更新步数为上一步 + 1。
2. 将要访问点在箱子上, 则箱子会被推动, 箱子的状态改变。
下面贴出代码 :
#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <algorithm>#include <iostream>#include <cmath>#include <ctime>#include <map>#include <vector>using namespace std;inline int read() { int i = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); } while(ch >= '0' && ch <= '9') { i = (i << 3) + (i << 1) + ch - '0'; ch = getchar(); } return i * f;}const int MAXQ = 6e6 + 10;int x[4] = {0, 1, 0, -1}, y[4] = {1, 0, -1, 0};int f[50][50][50][50], n, m, box[4], goal[4];int a[20][20], px, py, num, tot, b[20][20];int c[MAXQ][4];inline int pos(int x, int y) { return (x - 1) * m + y;}inline int gety(int x) { return x % m;}inline int getx(int x) { int now = gety(x); return (x - now) / m + 1;}inline void solve(int x, int y) { if(a[x][y] == 4) px = x, py = y, a[x][y] = 0; if(a[x][y] == 2) box[++num] = pos(x, y); if(a[x][y] == 3) goal[++tot] = pos(x, y);}inline void insert(int id, int x, int y, int z, int w) { c[id][0] = x; c[id][1] = y; c[id][2] = z; c[id][3] = w;}int main() { n = read(), m = read(); for(int i = 1; i <= n; ++i) { for(int j = 1; j <= m; ++j) a[i][j] = getchar() - '0', solve(i, j), b[i][j] = 1; getchar(); } memset(f, 255, sizeof(f)); f[pos(px, py)][box[1]][box[2]][box[3]] = 0; insert(1, pos(px, py), box[1], box[2], box[3]); for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j) if(a[i][j] == 1) b[i][j] = 0; int head = 1, tail = 1; for(; head <= tail; ++head) { int ren = c[head][0], box1 = c[head][1], box2 = c[head][2], box3 = c[head][3]; int xren = getx(ren), yren = gety(ren), x1 = getx(box1), y1 = gety(box1), x2 = getx(box2), y2 = gety(box2), x3 = getx(box3), y3 = gety(box3); b[x1][y1] = b[x2][y2] = b[x3][y3] = 0; int sx = f[ren][box1][box2][box3]; if(box1 == goal[1] && box2 == goal[2] && box3 == goal[3]) { printf("%d\n", sx); return 0; } for(int i = 0; i < 4; ++i) { int p = pos(xren + x[i], yren + y[i]); //if(b[xren + x[i]][yren + y[i]] == 0) continue; if(b[xren + x[i]][yren + y[i]] && f[p][box1][box2][box3] == -1) { f[p][box1][box2][box3] = sx + 1; insert(++tail, p, box1, box2, box3); } int q[4]; q[1] = box1, q[2] = box2, q[3] = box3; if(p == box2) swap(q[1], q[2]); if(p == box3) swap(q[1], q[3]); if(p == q[1]) if(b[xren + 2 * x[i]][yren + 2 * y[i]]) { // 这里对情况进行剪枝只维护 box 数列从小到大依次排列的情况 int nowp = pos(xren + 2 * x[i], yren + 2 * y[i]); q[1] = nowp; sort(q + 1, q + num + 1); if(f[p][q[1]][q[2]][q[3]] == -1) { f[p][q[1]][q[2]][q[3]] = sx + 1; insert(++tail, p, q[1], q[2], q[3]); } } } b[x1][y1] = b[x2][y2] = b[x3][y3] = 1; }}
本题结束
感谢阅读本篇文章,喜欢的话,点个赞吧,你的鼓励就是我最大的动力
有什么意见,尽情发表吧。
- [NOIP模拟]BOX(推箱子)-BFS
- hdu1254推箱子(BFS)
- 推箱子(双重bfs)
- hdu1254 推箱子--BFS
- Push Box(推箱子)
- HDOJ 1254 推箱子 (BFS)
- hdu1254 推箱子 (bfs+dfs)
- hdu 1254 推箱子(BFS)
- HDU1254 推箱子 双BFS
- hdoj1254推箱子【dfs+bfs】
- hdu1254 推箱子 【bfs+dfs】
- hdu 1254 推箱子 BFS
- HDU1254 推箱子(BFS)
- hdu 1254 推箱子(bfs + bfs)
- hdoj 1254 推箱子(BFS + BFS)
- hdu1254推箱子 (BFS+BFS+状态压缩)
- hdu1254 推箱子(bfs+bfs)
- HDU 1254 推箱子 (BFS套BFS)
- 安装theano模块
- jeesite Integer类型变成String类型的原因
- CRNN论文翻译——中英文对照
- centos 6.8 + pgsql 9.6 + file_fdw
- Android6.0加载Xutils-2.6.14.jar出现retry error, curr request is null 解决办法
- [NOIP模拟]BOX(推箱子)-BFS
- Python字符串格式化符号
- iOS-类和对象的见解
- Android Dalvik虚拟机初识
- Centos 多版本python管理
- Maven的应用场景
- Python WorkerThread设计实现
- 调用相机拍照并展示
- ConflictingBeanDefinitionException: Annotation-specified bean name 'errorPageFilter'