Push Box (BFS)
来源:互联网 发布:动漫手办淘宝 编辑:程序博客网 时间:2024/06/08 19:04
Push Box
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total Submission(s): 167 Accepted Submission(s): 81Problem Description
Push Box is a classic puzzle game. This game play in a grid, there are five types of block in it, the player, the box, the hole, empty place, and the wall. In every step, player can move up, down, left, or right, if the target place is empty. Moreover, if a box in the target place, and the next place in that direction is empty, player can move to the target place, and then push the box to the next place. Remember, both of the player and boxes can't move out of the grid, or you may assume that there is a wall suround the whole grid. The objective of this game is to push every box to a hole. Now, your problem is to find the strategy to achieve the goal with shortest steps, supposed there are exactly three boxes.
Input
The input consists of several test cases. Each test case start with a line containing two number, n, m(1 < n, m ≤ 8), the rows and the columns of grid. Then n lines follow, each contain exact m characters, representing the type of block in it. (for empty place, X for player, * for box, # for wall, @ for hole). Each case contain exactly one X, three *, and three @. The input end with EOF.
Output
You have to print the length of shortest strategy in a single line for each case. (-1 if no such strategy)
Sample Input
4 4......*@..*@.X*@6 6...#@.@..*..#*##....##*#..X....@#...
Sample Output
711
#include <iostream>#include <sstream>#include <string.h>#include <cstdio>#include <string>#include <cctype>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <set>#include <cmath>#define PI acos(-1)#define ll long long#define INF 0x7fffffffusing namespace std;#define N 9struct Node { int x[4]; int y[4]; int step;};/// 开始结束NodeNode s, t;int n, m;char G[N][N];///需要三个箱子和人位置确定才能确定状态bool v[N][N][N][N][N][N][N][N];int dx[4] = {0, 1, 0, -1};int dy[4] = {1, 0, -1, 0};bool check(int x, int y) { return (x >= 0 && x < n && y >= 0 && y < m && G[x][y] != '#');}int bfs() { memset(v, 0, sizeof(v)); Node now, next; now = s; now.step = 0; v[now.x[0]][now.y[0]][now.x[1]][now.y[1]][now.x[2]][now.y[2]][now.x[3]][now.y[3]] = 1; queue<Node> q; q.push(now); while (q.size()) { now = q.front(); q.pop(); if (G[now.x[0]][now.y[0]] == '@' && G[now.x[1]][now.y[1]] == '@' && G[now.x[2]][now.y[2]] == '@') { return now.step; } for (int i = 0; i < 4; i++) { next = now; next.x[3] += dx[i]; next.y[3] += dy[i]; /// 越界碰墙 if (check(next.x[3], next.y[3]) == 0) continue; /// 判断人是否走到箱子位子上 int flag = 0; for (int j = 0; j < 3; j++) { if (next.x[3] == next.x[j] && next.y[3] == next.y[j]) { /// 箱子往前移动 next.x[j] += dx[i]; next.y[j] += dy[i]; /// 箱子是否越界或碰墙 if (check(next.x[j], next.y[j]) == 0) { flag = 1; break; } /// 箱子是否移到另一个箱子位置处 for (int k = 0; k < 3; k++) { if (j != k && next.x[k] == next.x[j] && next.y[k] == next.y[j]) { flag = 1; break; } } } } if (flag) continue; /// 如果这种状态发生过 if (v[next.x[0]][next.y[0]][next.x[1]][next.y[1]][next.x[2]][next.y[2]][next.x[3]][next.y[3]] == 1) continue; /// 加入队列 v[next.x[0]][next.y[0]][next.x[1]][next.y[1]][next.x[2]][next.y[2]][next.x[3]][next.y[3]] = 1; next.step = now.step+1; q.push(next); } } return -1;}int main() { while (~scanf("%d%d", &n, &m)) { int t = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> G[i][j]; if (G[i][j] == 'X') { s.x[3] = i; s.y[3] = j; } else if (G[i][j] == '*') { s.x[t] = i; s.y[t++] = j; } } } cout << bfs() << endl; } return 0;}
0 0
- hdu1732 Push Box (bfs)
- hdu1732 Push Box ---- BFS
- Push Box (BFS)
- hdu 1732 Push Box(BFS)
- HDU---1732 Push Box【BFS】
- HDU-1732 Push Box BFS + BFS
- bfs hdu 1732 push the box
- hdu 1732 Push Box(bfs)
- HDU 1732 Push Box(BFS)
- hdu 1732 Push Box(BFS)
- hdu 1732 Push box 惨不忍睹的bfs代码。。。。
- HDU 1732——Push Box(BFS)
- HDU 1732 Push Box(BFS 拉链哈希)
- Push Box
- Push Box
- HDU 1732 push Box
- HDU Push Box
- HDU 1732 Push Box
- ☆☆深度分析:物联网OS两强之争和中国厂商的机遇
- 怎样 隐藏光标—设置光标
- 2017.1.28打卡 距NOI184天 cf体验(雾)
- android控件之ZoonControls(缩放控件)的使用
- 解释型语言的特点
- Push Box (BFS)
- SPI在linux3.14.78 FS_S5PC100(Cortex A8)和S3C2440上驱动移植(deep dive)
- 微软技术大会
- SPI在linux3.14.78 FS_S5PC100(Cortex A8)和S3C2440上驱动移植(deep dive)_0
- java模拟点击
- CCF201612-1 中间数(解法二)(100分)
- C陷阱与缺陷--笔记
- JS预解析
- BinarySearch