HDOJ 1254 推箱子【bfs && dfs】

推箱子

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


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
  
  
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

测试数据:

7

5 5
3 0 1 0 0
1 0 1 4 0
0 0 1 0 0
1 0 2 0 0
0 0 0 0 0
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
5 5
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 2 1 1
4 0 3 0 0
输出:

-1

4

5


#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
int m,n,flag;
int mazz[10][10];
bool vis[10][10][10][10];
bool fot[10][10];
int dir[4][2]={0,1,0,-1,1,0,-1,0};
struct node
{
    int x,y,rx,ry,t;
    friend bool operator < (node a,node b)
    {
    	return a.t > b.t;
    }
};
int judge(int x,int y,int rx,int ry)
{
	if(vis[x][y][rx][ry])
		return 0;
    if(x<0||x>=m||y<0||y>=n)
        return 0;
    if(mazz[x][y]!=0)
        return 0;
    return 1;
}
int j_udge(int x,int y)
{
	if(fot[x][y])
		return 0;
	if(x<0||x>=m||y<0||y>=n)
		return 0;
	if(mazz[x][y]!=0)
		return 0;
	return 1;
}
void dfs(int sx,int sy,int ex,int ey)
{
	if(sx==ex&&sy==ey)
	{
		flag=1;
		return ;
	}
	int nx,ny;
	for(int i=0;i<4;++i)
	{
		nx=sx+dir[i][0];
		ny=sy+dir[i][1];
		if(j_udge(nx,ny))
		{
			fot[nx][ny]=true;
			dfs(nx,ny,ex,ey);
		}
	}
}
int solve(int x,int y,int sx,int sy,int ex,int ey)
{
    memset(fot,false,sizeof(fot));
    fot[x][y]=true;
    fot[sx][sy]=true;
    flag=0;
    dfs(sx,sy,ex,ey);
    if(flag)
    	return 1;
    else 
    	return 0;
}
void bfs(int x,int y,int sx,int sy,int ex,int ey)
{
    node now,ss,next;
    ss.x=sx;
    ss.y=sy;
    ss.rx=x;
    ss.ry=y;
    ss.t=0;
    memset(vis,false,sizeof(vis));
    vis[sx][sy][x][y]=true;
    priority_queue<node>q;
    q.push(ss);
    while(!q.empty())
    {
        now=q.top();
        q.pop();
        //printf("x=%d y=%d\n",now.x,now.y);
        if(now.x==ex&&now.y==ey)
    	{
    		printf("%d\n",now.t);
    		return ;
    	}
        for(int i=0;i<4;++i)
        {
            next.x=now.x+dir[i][0];
            next.y=now.y+dir[i][1];
            next.rx=now.x-dir[i][0];
            next.ry=now.y-dir[i][1];
            //printf("%d %d j=%d s=%d\n",dir[i][0],dir[i][1],judge(next.x,next.y),solve(now.x,now.y,now.rx,now.ry,next.rx,next.ry));
            if(judge(next.x,next.y,next.rx,next.ry)&&solve(now.x,now.y,now.rx,now.ry,next.rx,next.ry))
			{
				next.t=now.t+1;
				vis[next.x][next.y][next.rx][next.ry]=true;
				q.push(next);
			}
        }
    }
    printf("-1\n");
}
int main()
{
    int t,x,y,sx,sy,ex,ey;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&m,&n);
        for(int i=0;i<m;++i)
        {
            for(int j=0;j<n;++j)
            {
                scanf("%d",&mazz[i][j]);
                if(mazz[i][j]==4)
                {
                    x=i,y=j;
                    mazz[i][j]=0;
                }
                if(mazz[i][j]==2)
                {
                    sx=i,sy=j;
                    mazz[i][j]=0;
                }
                if(mazz[i][j]==3)
                {
                    ex=i,ey=j;
                    mazz[i][j]=0;
                }
            }
        }
		bfs(x,y,sx,sy,ex,ey);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值