HDU1254-推箱子

75 篇文章 0 订阅
28 篇文章 0 订阅

推箱子

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 71   Accepted Submission(s) : 31
Font: Times New Roman | Verdana | Georgia
Font Size:  

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

Author

Ignatius.L & weigang Lee


解题思路:bfs+dfs,对箱子进行bfs,对人进行dfs


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <bitset>
#include <stack>
#include <map>
#include <climits>
#include <functional>

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;

int xx[8]={-1,0,1,0,-1,0,1,0};
int yy[8]={0,-1,0,1,0,-1,0,1};

bool mp[10][10][5];
int a[10][10],b[10][10];
int x1,y1,x2,y2,tx,ty,px,py;
int N,M;
bool flag;

struct node
{
    int x,y,px,py,step;
}n,m;

bool dfs(int x,int y)
{
    if(x==tx&&y==ty) return true;
    if(x<0||x>=N||y<0||y>=M) return false;
    if(x==m.x&&y==m.y) return false;
    if(b[x][y]==1||a[x][y]==1) return false;
    b[x][y]=1;
    return (dfs(x+1,y)||dfs(x-1,y)||dfs(x,y+1)||dfs(x,y-1));
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d %d", &N, &M);
        for(int i = 0; i < N; i++)
        {
            for(int j = 0; j < M; j++)
            {
                scanf("%d",&a[i][j]);
                b[i][j]=a[i][j];
                if(a[i][j]==2) x1=i,y1=j;
                if(a[i][j]==3) x2=i,y2=j;
                if(a[i][j]==4) px=i,py=j;
            }
        }
        memset(mp,false,sizeof mp);
        flag=false;
        n.x=x1;n.y=y1;n.step=0;
        n.px=px;n.py=py;
        queue<node>q;
        q.push(n);
        while(!q.empty())
        {
            m=q.front();
            q.pop();
            if(m.x==x2&&m.y==y2)
            {
                flag=true;
                break;
            }
            for(int i=0;i<4;i++)
            {
                n.x=m.x+xx[i];
                n.y=m.y+yy[i];
                tx=m.x+xx[i+2];
                ty=m.y+yy[i+2];
                n.step=m.step+1;
                if(n.x>=0&&n.x<N&&n.y>=0&&n.y<M&&!mp[n.x][n.y][i]&&a[n.x][n.y]!=1)
                {
                    memset(b,0,sizeof b);
                    if(tx>=0&&tx<N&&ty>=0&&ty<M&&a[tx][ty]!=1&&dfs(m.px,m.py))
                    {
                        n.px=tx;n.py=ty;
                        mp[n.x][n.y][i]=true;
                        q.push(n);
                    }
                }
            }
        }
        if(flag) printf("%d\n",m.step);
        else printf("-1\n");
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值