天天写算法之(BFS)推箱子

本文介绍了一种解决经典推箱子游戏问题的算法实现。通过使用C++编程语言,该算法利用了广度优先搜索(BFS)来寻找从初始状态到达目标状态的最短路径。文章详细展示了如何为游戏角色(人和箱子)定义状态节点,并通过递归搜索来更新状态,最终找到解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

地址: 点击打开链接
主要是需要考虑,箱子可以到,人是不是也可以到推箱子的位置。
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<string.h>
using namespace std;
int T,m,n;
int Map[10][10];
int flagBox[10][10][4];
int flagPerson[10][10];
int targetX ,targetY ;
int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}};
struct Node{
    int x ;
    int y ;
    int step ;
    int mmap[10][10];
    bool check()
    {
        if(x<0||x>=m||y<0||y>=n)
            return false ;
        return true ;
    }

}BoxStart,PersonStart,target;


bool bfs_person(Node n1){
    queue<Node> que ;
    PersonStart = n1 ;
    Node b ;
    for(int i = 0 ; i < m ; i ++)
    {
        for(int j = 0 ; j < n ; j ++)
        {
            if(n1.mmap[i][j]==4)
            {
                PersonStart.x = i ;
                PersonStart.y = j ;
                PersonStart.step = 0 ;
            }
        }
    }
    memset(flagPerson,0,sizeof(flagPerson));
    flagPerson[PersonStart.x][PersonStart.y]=1;
    if(PersonStart.x == target.x&&PersonStart.y==target.y)
        return true ;
    que.push(PersonStart);
    while(!que.empty())
    {
        Node a = que.front();
        que.pop();
        for(int i = 0 ; i < 4 ; i ++)
        {
            b = a ;
            b.step ++ ;
            b.x +=dir[i][0];
            b.y +=dir[i][1];
            if(!b.check()||b.mmap[b.x][b.y]==1||b.mmap[b.x][b.y]==2||flagPerson[b.x][b.y])
                continue ;
            flagPerson[b.x][b.y]=1;
            if(b.x == target.x&&b.y==target.y)
            {
                return true ;
            }

            que.push(b);
        }
    }
    return false ;
}



int BFS(){
    memset(flagBox,0,sizeof(flagBox));
    queue<Node> temp ;
    Node s,t ;
    temp.push(BoxStart);

    while(!temp.empty())
    {
        s = temp.front();
        temp.pop();
        for(int i =0 ; i < 4 ; i ++)
        {
            t = s ;
            t.x+=dir[i][0];
            t.y+=dir[i][1];
            t.step++;

            if(!t.check()||Map[t.x][t.y]==1||flagBox[t.x][t.y][i])
                continue ;
            target.x = s.x-dir[i][0];
            target.y = s.y-dir[i][1];

            if(!target.check())
                continue ;

            if(bfs_person(t))
            {
                 //   cout <<"  " <<target.x <<"  " << target.y << "  " << endl;
                swap(t.mmap[s.x][s.y],t.mmap[t.x][t.y]);
                swap(t.mmap[PersonStart.x][PersonStart.y],t.mmap[target.x][target.y]);
               // cout <<t.x << "  " << t.y <<" " <<t.step <<endl;
                flagBox[t.x][t.y][i]=1;
                if(Map[t.x][t.y]==3)
                    return t.step;

                temp.push(t);
            }
        }
    }
    return -1 ;
}


int main(){
    int i , j ;
    scanf("%d",&T);
    while(T--)
    {

        scanf("%d%d",&m,&n);
        for(i = 0 ; i < m; i ++)
        {
            for(j = 0 ; j < n ; j ++)
            {
                scanf("%d",&Map[i][j]);
                BoxStart.mmap[i][j] = Map[i][j] ;
                if(Map[i][j]==2)
                {
                    BoxStart.x = i ;
                    BoxStart.y = j ;
                    BoxStart.step = 0 ;
                }
            }
        }
        cout << BFS()<<endl ;
    }
    return 0 ;
}

´问题描述: 码头仓库是划分为n×m个格子的矩形阵列。有公共边的格子是相邻格子。当前仓库中 有的格子是空闲的;有的格子则已经堆放了沉重的货物。由于堆放的货物很重,单凭仓库管 理员的力量是无法移动的。仓库管理员有一项任务,要将一个小箱子推到指定的格子上去。 管理员可以在仓库中移动,但不能跨过已经堆放了货物的格子。管理员站在与箱子相对的空 闲格子上时,可以做一次推动,把箱子推到另一相邻的空闲格子。推箱时只能向管理员的对 面方向推。由于要推动的箱子很重,仓库管理员想尽量减少推箱子的次数。 ´编程任务: 对于给定的仓库布局,以及仓库管理员在仓库中的位置和箱子的开始位置和目标位置, 设计一个解推箱子问题的分支限界法, 计算出仓库管理员将箱子从开始位置推到目标位置所 需的最少推动次数。 ´数据输入: 由文件input.txt提供输入数据。输入文件第 1 行有 2个正整数 n和 m(1<=n,m<=100) , 表示仓库是n×m个格子的矩形阵列。接下来有 n行,每行有 m个字符,表示格子的状态。 S 表示格子上放了不可移动的沉重货物; w 表示格子空闲; M 表示仓库管理员的初始位置; P 表示箱子的初始位置; K 表示箱子的目标位置。 ´结果输出: 将计算出的最少推动次数输出到文件 output.txt。如果仓库管理员无法将箱子从开始位 置推到目标位置则输出“No solution!” 。 输入文件示例 输出文件示例 input.txt output.txt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值