您的位置:首页 > 其它

hdu 1254(搜索题)

2016-06-14 21:04 176 查看

推箱子

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


[align=left]Problem Description[/align]

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

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



[align=left]Input[/align]

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

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

[align=left]Sample Input[/align]

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

[align=left]Sample Output[/align]

4

[align=left]Author[/align]
[align=left] [/align]
[align=left]好多坑的一道题。。。很好的一道题。。。参考了别人的代码。。真的做不出来。。。每次结点存个新的图。。get。。。做了才知道。。。[/align]

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
int n,m;
int graph[8][8];
bool flag[8][8][4];
bool vis[8][8];
int dir[][2] = {{1,0},{-1,0},{0,1},{0,-1}};
struct Node
{
int x,y;
int step;
int g[10][10];
}s,person,t;
bool check(int x,int y)
{
if(x<0||x>=n||y<0||y>=m) return false;
return true;
}
bool bfs2(Node s,Node target){
person = s;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(person.g[i][j]==4){
person.x = i;
person.y = j;
person.step = 0;
}
}
}
queue<Node>q;
memset(vis,false,sizeof(vis));
q.push(person);
vis[person.x][person.y] = true;
while(!q.empty()){
Node now = q.front();
q.pop();
if(now.x==target.x&&now.y==target.y){
return true;
}
Node next;
for(int i=0;i<4;i++){
next.step = now.step+1;
next.x = now.x +dir[i][0];
next.y = now.y + dir[i][1];
if(!check(next.x,next.y)||s.g[next.x][next.y]==2||vis[next.x][next.y]||s.g[next.x][next.y]==1) continue;
vis[next.x][next.y] = true;
q.push(next);
}
}
return false;
}
int bfs(){
memset(flag,false,sizeof(flag));
queue <Node> q;
q.push(s);
while(!q.empty()){
Node now = q.front();
q.pop();
if(now.x==t.x&&now.y==t.y) return now.step;
Node next,target;
for(int i=0;i<4;i++){
next = now;
next.x = now.x+dir[i][0];
next.y = now.y+dir[i][1];
next.step = now.step+1;
if(!check(next.x,next.y)||flag[next.x][next.y][i]||next.g[next.x][next.y]==1) continue; ///该方向已经走过了
///人的目标点(人以此点为目标点将箱子从now推向next)
target.x = now.x - dir[i][0];
target.y = now.y - dir[i][1];
if(!check(target.x,target.y)) continue;
if(bfs2(next,target)){
swap(next.g[now.x][now.y],next.g[next.x][next.y]);
swap(next.g[target.x][target.y],next.g[person.x][person.y]);
flag[next.x][next.y][i] = true;
q.push(next);
}
}
}
return -1;
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--)
{
scanf("%d%d",&n,&m);
for(int i=0; i<n; i++)
for(int j=0; j<m; j++){
scanf("%d",&s.g[i][j]);
if(s.g[i][j]==2){
s.x = i;
s.y = j;
s.step = 0;
}
if(s.g[i][j]==3){
t.x = i;
t.y = j;
}
}
int res = bfs();
printf("%d\n",res);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: