HDU 1254 推箱子(BFS + DFS)
来源:互联网 发布:windjview mac 编辑:程序博客网 时间:2024/05/18 02:12
推箱子
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8018 Accepted Submission(s): 2315
Problem Description
推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能拉箱子,因此如果箱子被推到一个角上(如图2)那么箱子就不能再被移动了,如果箱子被推到一面墙上,那么箱子只能沿着墙移动.
现在给定房间的结构,箱子的位置,搬运工的位置和箱子要被推去的位置,请你计算出搬运工至少要推动箱子多少格.
现在给定房间的结构,箱子的位置,搬运工的位置和箱子要被推去的位置,请你计算出搬运工至少要推动箱子多少格.
Input
输入数据的第一行是一个整数T(1<=T<=20),代表测试数据的数量.然后是T组测试数据,每组测试数据的第一行是两个正整数M,N(2<=M,N<=7),代表房间的大小,然后是一个M行N列的矩阵,代表房间的布局,其中0代表空的地板,1代表墙,2代表箱子的起始位置,3代表箱子要被推去的位置,4代表搬运工的起始位置.
Output
对于每组测试数据,输出搬运工最少需要推动箱子多少格才能帮箱子推到指定位置,如果不能推到指定位置则输出-1.
Sample Input
15 50 3 0 0 01 0 1 4 00 0 1 0 01 0 2 0 00 0 0 0 0
Sample Output
4
Author
Ignatius.L & weigang Lee
Recommend
Ignatius.L | We have carefully selected several similar problems for you: 1180 1253 1026 1072 1240
思路;BFS 搜索箱子的移动,DFS搜索人是否能到达推箱子所需要的位置,注意标记箱子走过的地方时,不能单纯的标记,因为箱子可能回到原来的位置时最优。
#include<stdio.h>#include<algorithm>#include<queue>#include<string.h>using namespace std;int dx[] = {1, -1, 0, 0};int dy[] = {0, 0, -1, 1};int n, m;int map[10][10], vis[10][10];bool used[10][10];bool judge(int x, int y){return (x >= 0 && y >= 0 && x < n && y < m);}bool DFS(int x, int y, int tx, int ty){used[x][y] = true;if(x == tx && y == ty) return true;for (int i = 0; i < 4; i++){int ax = x + dx[i];int ay = y + dy[i];if(!judge(ax, ay) || map[ax][ay] == 1 || used[ax][ay]) continue;if(DFS(ax, ay, tx, ty)) return true;}return false; }struct node{int x, y, mx, my, step; };int bx, by, sx, sy;int BFS(int x, int y){queue<node>q;memset(vis, 0, sizeof(vis));node now, next;now.x = x;now.y = y;now.step = 0;vis[now.x][now.y] = 1;now.mx = sx;now.my = sy;q.push(now);while(!q.empty()){now = q.front();q.pop();if(map[now.x][now.y] == 3){return now.step;} for(int i = 0; i < 4; i++){next.x = now.x + dx[i];//箱子移动的位置 next.y = now.y + dy[i];if(!judge(next.x, next.y) || vis[next.x][next.y] > 2 || map[next.x][next.y] == 1) continue; int xx, yy;switch(i)//人要推箱子,需要到的位置 {case 0: xx = now.x + dx[1]; yy = now.y + dy[1]; break;case 1: xx = now.x + dx[0]; yy = now.y + dy[0]; break;case 2: xx = now.x + dx[3]; yy = now.y + dy[3]; break;case 3: xx = now.x + dx[2]; yy = now.y + dy[2]; break; }if(!judge(xx, yy) || map[xx][yy] == 1) continue;int flag = map[now.x][now.y];map[now.x][now.y] = 1;//注意 memset(used, false, sizeof(used));if(DFS(now.mx, now.my, xx, yy))//人是否能到达推箱子的位置 {next.step = now.step + 1;vis[next.x][next.y]++;next.mx = now.x;next.my = now.y;q.push(next);} map[now.x][now.y] = flag;}}return -1;}int main(){int t;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", &map[i][j]);if(map[i][j] == 2) {bx = i;by = j;}if(map[i][j] == 4){sx = i;sy = j;}}}printf("%d\n", BFS(bx, by));}return 0;}
0 0
- HDU 1254 - 推箱子(BFS + DFS)
- HDU 1254推箱子(bfs+dfs)
- HDU 1254 推箱子(BFS + DFS)
- hdu 1254 推箱子 (bfs +dfs)
- 【BFS+DFS】hdu 1254 推箱子
- HDU 1254:推箱子【DFS && BFS】
- HDU 1254 推箱子 (BFS + DFS)
- hdu 1254 推箱子 (bfs+dfs+预处理)
- HDU 1254 (经典游戏)推箱子 BFS+dfs
- hdu 1254 推箱子(BFS+BFS或BFS+DFS)
- hdu 1254 推箱子(bfs + bfs)
- bfs+bfs/dfs hdu 1254 推箱子1
- HDU 1254 推箱子 (bfs嵌套)
- hdu 1254推箱子(两次bfs)
- 题解: HDU 1254 :推箱子 (BFS)
- HDU 1254 推箱子(双重BFS)
- HDU 1254 推箱子(双BFS)
- HDU- 1254 推箱子(BFS)
- Azure Token Lifetime
- android--Intent和IntentFilter详解
- Ehcache工具类 EhcacheUtil
- HTML之认识标签(一)
- Codeforces Round #161 (Div. 2)-D. Cycle in Graph
- HDU 1254 推箱子(BFS + DFS)
- 【SpringBoot】SpringBoot核心-基本配置
- appium(二)简单的demo
- ArcGIS空间分析要点学习
- Fragment Transactions & Activity State Loss
- 5分钟学会两种侧滑菜单栏
- Hadoop、Spark等5种大数据框架对比,你的项目该用哪种?
- linux下生成动态库和静态库的方法
- mongodb配置服务启动