Bloxorz I POJ - 3322

题目链接:Bloxorz I POJ - 3322

===================================================

Bloxorz I

Time Limit: 2000MS Memory Limit: 65536K

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.
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
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

===================================================

题意:游戏推一个由两个正方形组成的长方体,可以横着推也可以把长方体竖起来,然后地图由空白、紧固地、不坚固地(长方体不能立在上面,但可以横着站在上面)组成;问长方体从开始位置到目标位置的最短路径

算法:BFS 简单模拟题

思路:

  • 其实这题主要是判重,因为包括竖着与横着两个状态,横着由两个坐标表示,又因为地图最大为500,如果判重数组由两个坐标来维护,vis[X1][Y1][X2][Y2],则是500的4次方,绝对MLE。

  • 这里就想到上一题Holedox Moving POJ - 1324的思路,只要记住一个坐标就行,另一个方块只要记住方向就可以了;然后判重数组就变为 vis[X1][Y1][方向]

  • 只要开三维,而且 500 * 500 * 5 绝对不MLE。这里我方向是:0——直立,1——右,2——下,3——左,4——上。

难点:

  • 这里我又是1A,模拟题可能就细节比较容易出错,不过如果你清楚思路,数据也过了,基本就ac了

===================================================

#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cstdio>

using namespace std;

const int INF = 0x3f3f3f3f;

int n,m;
int ditu[505][505];
bool vis[505][505][5];

int xx[] = {0,1,0,-1};
int yy[] = {1,0,-1,0};

//dir 0:直立 1~4 左下右上
int sX,sY,dir;
int tX,tY;

int check(int x,int y,int a,int b){
    if(a-x==0&&b-y==1) return 1;
    if(a-x==1&&b-y==0) return 2;
    if(a-x==0&&b-y==-1) return 3;
    if(a-x==-1&&b-y==0) return 4;
}

void init(){
    memset(ditu,0,sizeof ditu);
    dir =-1;
    for(int i=1;i<=n;i++)
    for(int j=1;j<=m;j++){
        char ch = getchar();if(ch=='\n') ch=getchar();
        if(ch=='X'){
            if(dir==-1) sX=i,sY=j,dir++;
            else dir=check(sX,sY,i,j);
            ditu[i][j]=1;
        }
        else if(ch=='O') tX=i,tY=j,ditu[i][j]=1;
        else if(ch=='.') ditu[i][j] = 1;
        else if(ch=='E') ditu[i][j] = 2;
    }
}

void show(){
    cout<<sX<<" "<<sY<<" "<<dir<<" "<<tX<<" "<<tY<<endl;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++) cout<<ditu[i][j]<<" ";cout<<endl;
    }
}

struct node{
    int x,y,d,step;
    node(int xx,int yy,int dd,int sstep):x(xx),y(yy),d(dd),step(sstep) {}
    bool check(){
        if(d==0&&x==tX&&y==tY) return true;
        return false;
    }
};

bool checkXY(int a,int b,int z){
    if(a<=0||a>n||b<=0||b>m) return false;
    if(ditu[a][b]==0) return false;
    if(z==0&&ditu[a][b]==2) return false;
    if(z==0) return true;
    int c,d;
    if(z==1) c=a,d=b+1;
    else if(z==2) c=a+1,d=b;
    else if(z==3) c=a,d=b-1;
    else if(z==4) c=a-1,d=b;
     if(c<=0||c>n||d<=0||d>m) return false;
    if(ditu[c][d]==0) return false;
    return true;
}

int BFS(){
    queue<node> q;
    q.push(node(sX,sY,dir,0));
    memset(vis,false,sizeof vis);
    vis[sX][sY][dir] = true;

    while(!q.empty()){
        node p = q.front();q.pop();
        if(p.check()) return p.step;
        //直立情况====================
        if(p.d==0){
            for(int i=0;i<4;i++){
                int x = p.x + xx[i], y = p.y + yy[i];
                int d = i + 1;
                if(!checkXY(x,y,d)) continue;
                if(vis[x][y][d]) continue;
                vis[x][y][d] = true;
                q.push(node(x,y,d,p.step+1));
            }
        }
        //方向为1===============
        if(p.d==1){
            int x,y,d;
             //右================
             x = p.x , y = p.y+2, d = 0;
            if(checkXY(x,y,d)&&!vis[x][y][d]){
                vis[x][y][d] = true;
                q.push(node(x,y,d,p.step+1));
            }
            //下=================
            x = p.x + 1, y = p.y ,d = 1;
            if(checkXY(x,y,d)&&!vis[x][y][d]){
                vis[x][y][d] = true;
                q.push(node(x,y,d,p.step+1));
            }
            //左================
             x = p.x , y = p.y-1, d = 0;
            if(checkXY(x,y,d)&&!vis[x][y][d]){
                vis[x][y][d] = true;
                q.push(node(x,y,d,p.step+1));
            }
            //上=================
            x = p.x - 1, y = p.y ,d = 1;
            if(checkXY(x,y,d)&&!vis[x][y][d]){
                vis[x][y][d] = true;
                q.push(node(x,y,d,p.step+1));
            }
        }
        //方向为2===============
        if(p.d==2){
            int x,y,d;
             //右================
             x = p.x , y = p.y+1, d = 2;
            if(checkXY(x,y,d)&&!vis[x][y][d]){
                vis[x][y][d] = true;
                q.push(node(x,y,d,p.step+1));
            }
            //下=================
            x = p.x + 2, y = p.y ,d = 0;
            if(checkXY(x,y,d)&&!vis[x][y][d]){
                vis[x][y][d] = true;
                q.push(node(x,y,d,p.step+1));
            }
            //左================
             x = p.x , y = p.y-1, d = 2;
            if(checkXY(x,y,d)&&!vis[x][y][d]){
                vis[x][y][d] = true;
                q.push(node(x,y,d,p.step+1));
            }
            //上=================
            x = p.x - 1, y = p.y ,d = 0;
            if(checkXY(x,y,d)&&!vis[x][y][d]){
                vis[x][y][d] = true;
                q.push(node(x,y,d,p.step+1));
            }
        }
        //方向为3===============
        if(p.d==3){
            int x,y,d;
             //右================
             x = p.x , y = p.y+1, d = 0;
            if(checkXY(x,y,d)&&!vis[x][y][d]){
                vis[x][y][d] = true;
                q.push(node(x,y,d,p.step+1));
            }
            //下=================
            x = p.x + 1, y = p.y ,d = 3;
            if(checkXY(x,y,d)&&!vis[x][y][d]){
                vis[x][y][d] = true;
                q.push(node(x,y,d,p.step+1));
            }
            //左================
             x = p.x , y = p.y-2, d = 0;
            if(checkXY(x,y,d)&&!vis[x][y][d]){
                vis[x][y][d] = true;
                q.push(node(x,y,d,p.step+1));
            }
            //上=================
            x = p.x - 1, y = p.y ,d = 3;
            if(checkXY(x,y,d)&&!vis[x][y][d]){
                vis[x][y][d] = true;
                q.push(node(x,y,d,p.step+1));
            }
        }
        //方向为4===============
        if(p.d==4){
            int x,y,d;
             //右================
             x = p.x , y = p.y+1, d = 4;
            if(checkXY(x,y,d)&&!vis[x][y][d]){
                vis[x][y][d] = true;
                q.push(node(x,y,d,p.step+1));
            }
            //下=================
            x = p.x + 1, y = p.y ,d = 0;
            if(checkXY(x,y,d)&&!vis[x][y][d]){
                vis[x][y][d] = true;
                q.push(node(x,y,d,p.step+1));
            }
            //左================
             x = p.x , y = p.y-1, d = 4;
            if(checkXY(x,y,d)&&!vis[x][y][d]){
                vis[x][y][d] = true;
                q.push(node(x,y,d,p.step+1));
            }
            //上=================
            x = p.x - 2, y = p.y ,d = 0;
            if(checkXY(x,y,d)&&!vis[x][y][d]){
                vis[x][y][d] = true;
                q.push(node(x,y,d,p.step+1));
            }
        }
    }
    return -1;
}

int main()
{
    while(cin>>n>>m&&n&&m){
        init();//show();
        int ans = BFS();
        if(ans==-1) puts("Impossible");
        else cout<<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

盐太郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值