poj 3322 Bloxorz I ( bfs+三维判重 )

Bloxorz I
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4808 Accepted: 1592

Description

Little Tom loves playing games. One day he downloads a little computer game called 'Bloxorz' which makes him excited. It's a game about rolling a box to a specific position on a special plane. Precisely, the plane, which is composed of several unit cells, is a rectangle shaped area. And the box, consisting of two perfectly aligned unit cube, may either lies down and occupies two neighbouring cells or stands up and occupies one single cell. One may move the box by picking one of the four edges of the box on the ground and rolling the box 90 degrees around that edge, which is counted as one move. There are three kinds of cells, rigid cells, easily broken cells and empty cells. A rigid cell can support full weight of the box, so it can be either one of the two cells that the box lies on or the cell that the box fully stands on. A easily broken cells can only support half the weight of the box, so it cannot be the only cell that the box stands on. An empty cell cannot support anything, so there cannot be any part of the box on that cell. The target of the game is to roll the box standing onto the only target cell on the plane with minimum moves.


The box stands on a single cell


The box lies on two neighbouring cells, horizontally


The box lies on two neighbouring cells, vertically

After Little Tom passes several stages of the game, he finds it much harder than he expected. So he turns to your help.

Input

Input contains multiple test cases. Each test case is one single stage of the game. It starts with two integers R and C(3 ≤ R, C ≤ 500) which stands for number of rows and columns of the plane. That follows the plane, which contains R lines and C characters for each line, with 'O' (Oh) for target cell, 'X' for initial position of the box, '.' for a rigid cell, '#' for a empty cell and 'E' for a easily broken cell. A test cases starts with two zeros ends the input.

It guarantees that

  • There's only one 'O' in a plane.
  • There's either one 'X' or neighbouring two 'X's in a plane.
  • The first(and last) row(and column) must be '#'(empty cell).
  • Cells covered by 'O' and 'X' are all rigid cells.

Output

For each test cases output one line with the minimum number of moves or "Impossible" (without quote) when there's no way to achieve the target cell.  

Sample Input

7 7
#######
#..X###
#..##O#
#....E#
#....E#
#.....#
#######
0 0

Sample Output

10

Source


ps:其实这是一个游戏  贴个链接  可以进去玩玩之后再做这题  很有感觉的 (不要玩上瘾呦)

http://user.qzone.qq.com/1019256391?ptlang=2052#!app=2&via=QZ.HashRefresh&pos=1309244406


思路:只要把箱子的状态表示出来了就好办了  模拟箱子滚动和判重都不是问题了


代码:

// 这题被自己坑了  异想天开的用bool表示  0 1 2
// 本来可以避免的错误  自己挖个坑往坑里跳 我晕  ╮(╯▽╰)╭
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#define maxn 510
using namespace std;

int n,m,ans,cnt,xxc;
int sx[2],sy[2],ex,ey;
char s[maxn];
int mp[maxn][maxn];
bool vis[4][maxn][maxn];         // 判重 箱子状态+左上角箱子所在坐标
struct Node
{
    int x[2],y[2];
    int step,type;
}cur,now;
queue<Node>q;

bool isok()          // 对cur进行判断即可
{
    int i,j,t,xx[2],yy[2];
    t=cur.type;
    xx[0]=cur.x[0];
    xx[1]=cur.x[1];
    yy[0]=cur.y[0];
    yy[1]=cur.y[1];
 //   printf("t:%d xx[0]:%d yy[0]:%d\n",t,xx[0],yy[0]);
    if(vis[t][xx[0]][yy[0]]) return false ;
    if(t==1)
    {
        if(mp[xx[0]][yy[0]]==0||mp[xx[0]][yy[0]]==2) return false ;   // 状态为一时需判断地图是否为 easily broken cell
    }
    else
    {
        for(i=0;i<2;i++)
        {
            if(mp[xx[i]][yy[i]]==0) return false ;
        }
    }
    return true ;
}
bool bfs()
{
    int i,j;
    int nx[2],ny[2],nstep,ntype;
    memset(vis,0,sizeof(vis));
    while(!q.empty()) q.pop();
    if(xxc)
    {
        if(sx[1]-sx[0]==1) cur.type=3;
        else cur.type=2;
        cur.x[0]=sx[0];
        cur.x[1]=sx[1];
        cur.y[0]=sy[0];
        cur.y[1]=sy[1];
    }
    else
    {
        cur.type=1;
        cur.x[0]=sx[0];
        cur.y[0]=sy[0];
    }
    cur.step=0;
    vis[cur.type][sx[0]][sy[0]]=1;
    q.push(cur);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        ntype=now.type;
        nstep=now.step;
        nx[0]=now.x[0];
        nx[1]=now.x[1];
        ny[0]=now.y[0];
        ny[1]=now.y[1];
        if(ntype==1&&nx[0]==ex&&ny[0]==ey)
        {
            ans=nstep;
            return true ;
        }
        cur.step=nstep+1;
        if(ntype==1)
        {
            cur.type=2;
            cur.y[0]=ny[0]-2;
            cur.y[1]=ny[0]-1;
            cur.x[0]=cur.x[1]=nx[0];
            if(isok())                     // 左
            {
                vis[2][cur.x[0]][cur.y[0]]=1;
                q.push(cur);
            }
            cur.y[0]=ny[0]+1;
            cur.y[1]=ny[0]+2;
            if(isok())                     // 右
            {
                vis[2][cur.x[0]][cur.y[0]]=1;
                q.push(cur);
            }
            cur.type=3;
            cur.x[0]=nx[0]-2;
            cur.x[1]=nx[0]-1;
            cur.y[0]=cur.y[1]=ny[0];
            if(isok())                     // 上
            {
                vis[3][cur.x[0]][cur.y[0]]=1;
                q.push(cur);
            }
            cur.x[0]=nx[0]+1;
            cur.x[1]=nx[0]+2;
            if(isok())                     // 下
            {
                vis[3][cur.x[0]][cur.y[0]]=1;
                q.push(cur);
            }
        }
        else if(ntype==2)
        {
            cur.type=2;
            cur.x[0]=cur.x[1]=nx[0]-1;
            cur.y[0]=ny[0];
            cur.y[1]=ny[1];
            if(isok())                     // 上
            {
                vis[2][cur.x[0]][cur.y[0]]=1;
                q.push(cur);
            }
            cur.x[0]=cur.x[1]=nx[0]+1;
            if(isok())                     // 下
            {
                vis[2][cur.x[0]][cur.y[0]]=1;
                q.push(cur);
            }
            cur.type=1;
            cur.x[0]=nx[0];
            cur.y[0]=ny[0]-1;
            if(isok())                     // 左
            {
                vis[1][cur.x[0]][cur.y[0]]=1;
                q.push(cur);
            }
            cur.y[0]=ny[0]+2;
            if(isok())                     // 右
            {
                vis[1][cur.x[0]][cur.y[0]]=1;
                q.push(cur);
            }
        }
        else
        {
            cur.type=1;
            cur.x[0]=nx[0]-1;
            cur.y[0]=ny[0];
            if(isok())                     // 上
            {
                vis[1][cur.x[0]][cur.y[0]]=1;
                q.push(cur);
            }
            cur.x[0]=nx[0]+2;
            if(isok())                     // 下
            {
                vis[1][cur.x[0]][cur.y[0]]=1;
                q.push(cur);
            }
            cur.type=3;
            cur.x[0]=nx[0];
            cur.x[1]=nx[1];
            cur.y[0]=cur.y[1]=ny[0]-1;
            if(isok())                     // 左
            {
                vis[3][cur.x[0]][cur.y[0]]=1;
                q.push(cur);
            }
            cur.y[0]=cur.y[1]=ny[0]+1;
            if(isok())                     // 右
            {
                vis[3][cur.x[0]][cur.y[0]]=1;
                q.push(cur);
            }
        }
    }
    return false ;
}
int main()
{
    int i,j;
    while(scanf("%d%d",&n,&m),n||m)
    {
        memset(mp,0,sizeof(mp));
        xxc=-1;
        for(i=1;i<=n;i++)
        {
            scanf("%s",s);
            for(j=1;j<=m;j++)
            {
                if(s[j-1]=='.')
                {
                    mp[i][j]=1;
                }
                else if(s[j-1]=='O')
                {
                    mp[i][j]=1;
                    ex=i;
                    ey=j;
                }
                else if(s[j-1]=='E')
                {
                    mp[i][j]=2;
                }
                else if(s[j-1]=='X')
                {
                    xxc++;
                    mp[i][j]=1;
                    sx[xxc]=i;
                    sy[xxc]=j;
                }
            }
        }
        if(bfs()) printf("%d\n",ans);
        else printf("Impossible\n");
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值