POJ3322 Bloxorz I(BFS)

很有意思的一个益智小游戏。题目我就不描述了,玩过的人都知道规则。
题目传送门:http://poj.org/problem?id=3322
空格子不能走(可以看成墙),标号为E表示脆弱的格子,箱子在上面不能竖放,只能横放。终点只能竖着放进去。
很明显是搜索题,状态表示也不用太费力去想。需要保存箱子一个角的位置,和摆放的状态f;如果箱子竖放,则位置就是它本身的位置,f=0;如果左右横放,则保存左端的位置,f=1,判断的时候要考虑右端格子;如果上下横放,则保存上端的位置,f=2,判断的时候要考虑下端格子。
然后按不同方向移动箱子的时候,f的值也需要作出相应的更改。

#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct Node
{
    int x,y,f,stp;
    Node(){}
    Node(int a,int b,int c,int d)
    {x = a; y = b; f = c; stp = d;}
}st;
int n,m,vis[501][501][3],ed[2],cas;
int dir1[4][2] = {{-2,0},{1,0},{0,-2},{0,1}};//竖放移动的方向
int dir2[4][2] = {{-1,0},{1,0},{0,-1},{0,2}};//左右横放移动的方向
int dir3[4][2] = {{-1,0},{2,0},{0,-1},{0,1}};//上下横放移动的方向
char mp[501][501];
bool inarea(int x,int y)
{
    if(x < 1||x > n||y < 1||y > m) return false;
    else return true;
}
int bfs()
{
    queue<Node> Q;
    Q.push(st);
    vis[st.x][st.y][st.f] = cas;
    while(!Q.empty())
    {
        Node u = Q.front();
        Q.pop();
        int tx,ty,tf;
        if(u.f == 0)
            for(int i = 0; i < 4; i++)
            {
                tx = u.x+dir1[i][0],ty = u.y+dir1[i][1];
                if(i == 0||i == 1) tf = 2;
                else tf = 1;
                if((i == 0||i == 1)&&(mp[tx][ty] == '#'||mp[tx+1][ty] == '#'||!inarea(tx,ty)||!inarea(tx+1,ty))) continue;
                if((i == 2||i == 3)&&(mp[tx][ty] == '#'||mp[tx][ty+1] == '#'||!inarea(tx,ty)||!inarea(tx,ty+1))) continue;
                if(vis[tx][ty][tf] == cas) continue;
                vis[tx][ty][tf] = cas;
                Q.push(Node(tx,ty,tf,u.stp+1));
            }
        if(u.f == 1)
            for(int i = 0; i < 4; i++)
            {
                tx = u.x+dir2[i][0],ty = u.y+dir2[i][1];
                if(i == 0||i == 1) tf = 1;
                else tf = 0;
                if(tx == ed[0]&&ty == ed[1]&&tf == 0) return u.stp+1;
                if((i == 0||i == 1)&&(mp[tx][ty] == '#'||mp[tx][ty+1] == '#'||!inarea(tx,ty)||!inarea(tx,ty+1))) continue;
                if((i == 2||i == 3)&&(mp[tx][ty] == '#'||mp[tx][ty] == 'E'||!inarea(tx,ty))) continue;//不能竖着放在脆弱的格子上面
                if(vis[tx][ty][tf] == cas) continue;
                vis[tx][ty][tf] = cas;
                Q.push(Node(tx,ty,tf,u.stp+1));
            }
        if(u.f == 2)
            for(int i = 0; i < 4; i++)
            {
                tx = u.x+dir3[i][0],ty = u.y+dir3[i][1];
                if(i == 0||i == 1) tf = 0;
                else tf = 2;
                if(tx == ed[0]&&ty == ed[1]&&tf == 0) return u.stp+1;
                if((i == 0||i == 1)&&(mp[tx][ty] == '#'||mp[tx][ty] == 'E'||!inarea(tx,ty))) continue;
                if((i == 2||i == 3)&&(mp[tx][ty] == '#'||mp[tx+1][ty] == '#'||!inarea(tx,ty)||!inarea(tx+1,ty))) continue;
                if(vis[tx][ty][tf] == cas) continue;
                vis[tx][ty][tf] = cas;
                Q.push(Node(tx,ty,tf,u.stp+1));
            }
    }
    return -1;
}
void Init()
{
    st.x = st.y = st.f = 0;
    cas++;
    memset(mp,0,sizeof mp);
}
int main()
{
    while(scanf("%d%d",&n,&m) != EOF&&n+m)
    {
        Init();
        for(int i = 1; i <= n; i++)
        {
            scanf("%s",mp[i]+1);
        }
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= m; j++)
            {   
                if(mp[i][j] == 'X')
                {
                    mp[i][j] = '.';
                    if(!st.y)
                    {
                        st.x = i,st.y = j,st.f = 0,st.stp = 0;
                    }
                    else if(st.y+1 == j) st.f = 1;
                    else st.f = 2;
                }
                if(mp[i][j] == 'O') ed[0] = i,ed[1] = j,mp[i][j] = '#';//终点格子也视作空格子,但是好像并没有什么影响
            }
        int ans = bfs();
        if(ans == -1) printf("Impossible\n");
        else printf("%d\n",ans);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值