推箱子

1254 推箱子

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 690    Accepted Submission(s): 181

Problem Description
推箱子是一个很经典的游戏.今天我们来玩一个简单版本.在一个M*N的房间里有一个箱子和一个搬运工,搬运工的工作就是把箱子推到指定的位置,注意,搬运工只能推箱子而不能拉箱子,因此如果箱子被推到一个角上(如图2)那么箱子就不能再被移动了,如果箱子被推到一面墙上,那么箱子只能沿着墙移动.

现在给定房间的结构,箱子的位置,搬运工的位置和箱子要被推去的位置,请你计算出搬运工至少要推动箱子多少格.

推箱子_第1张图片
 

 

Input
输入数据的第一行是一个整数T(1<=T<=20),代表测试数据的数量.然后是T组测试数据,每组测试数据的第一行是两个正整数M,N(2<=M,N<=7),代表房间的大小,然后是一个M行N列的矩阵,代表房间的布局,其中0代表空的地板,1代表墙,2代表箱子的起始位置,3代表箱子要被推去的位置,4代表搬运工的起始位置.
 

 

Output
对于每组测试数据,输出搬运工最少需要推动箱子多少格才能帮箱子推到指定位置,如果不能推到指定位置则输出-1.
 

 

Sample Input
1
5 5
0 3 0 0 0
1 0 1 4 0
0 0 1 0 0
1 0 2 0 0
0 0 0 0 0
 

 

Sample Output
4

1732 Push Box

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)
Total Submission(s): 233    Accepted Submission(s): 109


Problem 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
7
11
 
//////////////////////////////////////////////////////////////////////////////////////////////////////////
#include<stdio.h> 1254: #include<iostream> #include<stdlib.h> using namespace std; #include<queue> struct MAN // 人与箱子合在一个结构体里! { int x; int y; int box_x; int box_y; int step; }; int dir[4][2] = {1,0,0,1,-1,0,0,-1}; int row,rank; int maze[10][10]; int hash[10][10][10][10]; //人与箱子一起hash 前面两个hash人的位置 后面两个hash箱子的位子 用步数hash MAN start; int BFS() //BFS主体 { int i,min = 9999; MAN now,next,boxnext; queue<MAN>man; man.push(start); while(!man.empty()) { now = man.front(); man.pop(); for(i=0; i<4; i++) { next.x = now.x + dir[i][0]; next.y = now.y + dir[i][1]; next.step = now.step; next.box_x = now.box_x; next.box_y = now.box_y; if(maze[next.x][next.y] == 1) continue; if(next.x <0 || next.x >= row || next.y < 0 || next.y >= rank) continue; if(hash[next.x][next.y][now.box_x][now.box_y] <=now.step) // 表示这个地方 人与箱子这样的位置存在过是 <= 当时少了个 = TLE了。。 continue; if(next.x == now.box_x && next.y == now.box_y ) // 遇到了箱子 推吧! { next.box_x = now.box_x + dir[i][0]; next.box_y = now.box_y + dir[i][1]; next.step = now.step + 1; if(maze[next.box_x][next.box_y] == 3) // 找到终点但不要直接返回 要找所有的可能性 然后找到推的最小次数(因为有可能走的步数最少 但推的次数很多 当时就死在这里!) { if(next.step < min) min = next.step; } if(maze[next.box_x][next.box_y] == 1) // 箱子到墙了 continue; if(next.box_x < 0 || next.box_x >= row || next.box_y < 0 || next.box_y >= rank) // 箱子推出界了 continue; hash[next.x][next.y][next.box_x][next.box_y] = next.step; // 将走过的hash掉 } else hash[next.x][next.y][next.box_x][next.box_y] = now.step; // 没推到箱子也hash man.push(next); } } return min; // 返回最小的次数 } int main () { int i,j,s,TEST,i1,i2,i3,i4,min; scanf("%d",&TEST); while(TEST--) { scanf("%d %d",&row, &rank); for(i1=0; i1<10; i1++) { for(i2=0; i2<10; i2++) { for(i3=0; i3<10; i3++) { for(i4=0; i4<10; i4++) hash[i1][i2][i3][i4] = 9999; // hash表初始化 } } } for(i=0; i<row; i++) { for(j=0; j<rank; j++) { scanf("%d",&maze[i][j]); if(maze[i][j] == 4) { start.x = i; start.y = j; } if(maze[i][j] == 2) { start.box_x = i; start.box_y = j; start.step = 0; } } } hash[start.x][start.y][start.box_x][start.box_y] = 0; // 起点hash step 为0 s = BFS(); if(s == 9999) //如果没有找到 输出-1 printf("-1/n"); else printf("%d/n",s); memset(maze,0,sizeof(maze)); //地图初始化这里我也死过一次。。。。 } return 0; }

你可能感兴趣的:(推箱子)