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

地址: 点击打开链接
主要是需要考虑,箱子可以到,人是不是也可以到推箱子的位置。
代码:
#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 ;
}

猜你喜欢

转载自blog.csdn.net/qq_36616268/article/details/80503417