Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 690 Accepted Submission(s): 181
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
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 DescriptionPush 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.
InputThe 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.
OutputYou have to print the length of shortest strategy in a single line for each case. (-1 if no such strategy)
Sample Input4 4 .... ..*@ ..*@ .X*@ 6 6 ...#@. @..*.. #*##.. ..##*# ..X... .@#...
Sample Output7 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; }