hdu1254 推箱子--BFS
来源:互联网 发布:知已知彼 编辑:程序博客网 时间:2024/06/09 07:17
原题链接: http://acm.hdu.edu.cn/showproblem.php?pid=1254
一:分析
分两步,一是箱子走到终点,二是人得走到箱子的前一个位置。
先是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;}
1 0
- hdu1254推箱子(BFS)
- hdu1254 推箱子--BFS
- hdu1254 推箱子 (bfs+dfs)
- HDU1254 推箱子 双BFS
- hdu1254 推箱子 【bfs+dfs】
- HDU1254 推箱子(BFS)
- hdu1254推箱子 (BFS+BFS+状态压缩)
- hdu1254 推箱子(bfs+bfs)
- HDU1254 推箱子 BFS+优先队列
- hdu1254 推箱子(广搜bfs)
- Hdu1254-推箱子(经典的推箱子游戏)-bfs
- hdu1254 推箱子 搜索水题(bfs+bfs)
- hdu1254 推箱子
- hdu1254推箱子
- hdu1254推箱子
- hdu1254推箱子
- hdu1254 推箱子
- HDU1254推箱子(AC)
- 通过缓冲区复制文本文件
- 多媒体技术基础
- 黑马程序员:开源Swift 3.0微博项目 ,做最具情怀的IT教育
- Java中List Set Map 是否有序等总结,两个对象equals算重复。
- ADT插件安装
- hdu1254 推箱子--BFS
- 移动端图片_编码解码调研
- java学习笔记
- 2016多校第四题 GCD(hdu 5726)(*)
- HDOJ 1997 汉诺塔VII(汉诺塔系列,判断排放是否正确)
- java连接mysql数据库
- runloop
- Android Studio快捷键
- linux 内核移植中的基本配置