POJ1475 Pushing Boxes(BFS+BFS)

题目就是推箱子的小游戏,叫你输出最优方案。推箱子用大写字母,人走用小写。
自己的想法:BFS乱搜。题解:双重BFS。涨姿势了,第一次见到。
外层BFS表示箱子要被推到的方向,内层BFS判断人是否能走到需要的那个位置;比如要往左推箱子,则要判断人是否能走到箱子的右边。
外层BFS还要开个数组判断是否在这个方向上经过这个点,不过我没理解透,估计再叫我写一遍我也写不出来。

#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<iostream>
using namespace std;
struct Node
{
    int x,y,px,py;//在外层BFS中分别表示人的位置,箱子位置
    string S;
    Node(){}
    Node(int a,int b,int c,int d)
    {x = a; y = b; px = c; py = d;}
};
Node st;
int n,m;
int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
char mp[25][25],P[4] = {'N','S','W','E'},M[4] = {'n','s','w','e'};
bool inarea(int i,int j)
{
    if(i < 1||i > n||j < 1||j > m) return false;
    else return true;
}
void init()
{
    memset(mp,0,sizeof mp);
}
string tmp;
bool vis[25][25];
bool bfsto(Node human,Node box)//内层bfs把人走到箱子的位置
{
    tmp = "";
    memset(vis,0,sizeof vis);
    queue<Node> q;
    Node sst;
    sst.x = human.px,sst.y = human.py;//人在的位置
    q.push(sst);
    while(!q.empty())
    {
        Node u = q.front(),v;
        q.pop();
        if(u.x == human.x&&u.y == human.y) //走到目标
        {   
            tmp = u.S;
            return true;
        }   
        for(int i = 0; i < 4; i++)
        {
            v.x = u.x+dir[i][0],v.y = u.y+dir[i][1],v.S = u.S;
            if(!inarea(v.x,v.y)||vis[v.x][v.y]) continue;
            if(mp[v.x][v.y] == '#') continue;
            if(v.x == box.x&&v.y == box.y) continue;//箱子的位置
            v.S += M[i],vis[v.x][v.y] = 1;
            q.push(v);
        }
    }
    return false;
}
bool passed[25][25][4];
void bfs()
{
    memset(passed,0,sizeof passed);
    queue<Node> Q;
    Q.push(st);
    while(!Q.empty())
    {
        Node u = Q.front(),next,human;
        Q.pop();
        if(mp[u.x][u.y] == 'T')
        {
            cout<<u.S<<endl<<endl;
            return;
        }
        for(int i = 0; i< 4; i++)
        {
            next = u,human = u;
            next.x = u.x+dir[i][0],next.y = u.y+dir[i][1];
            if(!inarea(next.x,next.y)) continue;
            if(passed[next.x][next.y][i]) continue;//在方向i上箱子是否经过这个位置
            if(mp[next.x][next.y] == '#') continue;
            if(i == 0) human.x = u.x+1;//向上推箱子,人要走到箱子的下边
            else if (i == 1) human.x = u.x-1;
            else if(i == 2) human.y = u.y+1;
            else human.y = u.y-1;
            if(!bfsto(human,u)) continue;
            passed[next.x][next.y][i] = 1;
            next.S = u.S+tmp;
            next.S += P[i];
            next.px = u.x,next.py = u.y;
            Q.push(next);
        }
    }
    printf("Impossible.\n\n");
}
int main()
{
    int cas = 1;
    while(scanf("%d%d",&n,&m) != EOF&&n+m)
    {
        init();
        st.S = "";
        for(int i = 1; i <= n; i++)
        {   
            scanf("%s",mp[i]+1);
            for(int j = 1; j <= m; j++)
            {   
                if(mp[i][j] == 'S') {st.px = i,st.py = j,mp[i][j] = '.';}
                if(mp[i][j] == 'B') {st.x = i,st.y = j,mp[i][j] = '.';}
            }   
        }
        printf("Maze #%d\n",cas++);
        bfs();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值