hdu1254 推箱子(bfs+bfs)

推箱子

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


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嵌套bfs,一个搜人的轨迹,一个搜箱子的轨迹。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const int MOD = 1000000007;
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))

int n,m;
int mat[11][11],dir[4][2]= {{0,1},{0,-1},{1,0},{-1,0}};
bool vis[11][11][4],vm[11][11];
int man_x,man_y,box_x,box_y,en_x,en_y;

struct A
{
    int x,y;
};
struct node
{
    int step;
    struct A box,man;
};
bool bb(int b_x, int b_y, int m_x, int m_y, int k)//搜人是否能到达要推的位置
{
    CL(vm, false);
    queue<node> qq;
    node ss,tt;
    ss.man.x=m_x;
    ss.man.y=m_y;
    vm[ss.man.x][ss.man.y]=true;
    vm[b_x][b_y]=true;//当前箱子的位置标记不可访问
    qq.push(ss);
    while(!qq.empty())
    {
        ss=qq.front();
        qq.pop();

        if(ss.man.x+dir[k][0]==b_x&&ss.man.y+dir[k][1]==b_y) return true;
        for(int i=0; i<4; i++)
        {
            tt.man.x=ss.man.x+dir[i][0];
            tt.man.y=ss.man.y+dir[i][1];
            if(mat[tt.man.x][tt.man.y]!=0) continue;
            if(!vm[tt.man.x][tt.man.y])
            {
                vm[tt.man.x][tt.man.y]=true;
                qq.push(tt);
            }
        }
    }
    return false;
}
void bfs()//搜箱子的轨迹
{
    queue<node> q;
    node now,next;
    now.box.x=box_x;
    now.box.y=box_y;
    now.man.x=man_x;
    now.man.y=man_y;
    now.step=0;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        if(now.box.x==en_x&&now.box.y==en_y)
        {
            cout<<now.step<<endl;
            return ;
        }
        for(int i=0; i<4; i++)
        {
            next.box.x=now.box.x+dir[i][0];
            next.box.y=now.box.y+dir[i][1];

            //cout<<bb(now.box.x, now.box.y, now.man.x, now.man.y, i)<<endl;
            if(vis[next.box.x][next.box.y][i]||mat[next.box.x][next.box.y]!=0) continue;

            if(bb(now.box.x, now.box.y, now.man.x, now.man.y, i))//如果人能走到推箱子的位置
            {
                vis[next.box.x][next.box.y][i]=true;
                next.man.x=now.box.x;//人推箱子后的位置变为之前箱子的位置
                next.man.y=now.box.y;
                next.step=now.step+1;
                q.push(next);
            }
        }
    }
    cout<<"-1"<<endl;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        CL(mat, -1);//地图初始化,边界全为-1
        cin>>n>>m;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=m; j++)
            {
                cin>>mat[i][j];
                if(mat[i][j]==2)//人、箱子和终点的位置存储好,再把地图中相应位置变为可走,即0
                {
                    box_x=i;
                    box_y=j;
                    mat[i][j]=0;
                }
                if(mat[i][j]==3)
                {
                    en_x=i;
                    en_y=j;
                    mat[i][j]=0;
                }
                if(mat[i][j]==4)
                {
                    man_x=i;
                    man_y=j;
                    mat[i][j]=0;
                }
            }
        }
        CL(vis, false);
        bfs();
    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值