POJ - 3322 Bloxorz I

Bloxorz I

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




解题报告:简单的BFS,与以往不同的是,这次不是一个一个格子的走,是一个或两个,只是代码写起来比较麻烦,其他都一样。vis数组采用vis[505][505][5],第三维的5代表5种状态,0代表在该格子是竖直向上,1代表向左横着,2代表向上竖着,3代表向右横着,4代表向下竖着。可以优化成3种状态,但是5种容易写代码。详见代码。

另外用cin会超时!!!!!!!!!


#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <map>
#include <string.h>
#include <algorithm>
#include <queue>

using namespace std;

int N,M;
char maze[505][505];
bool vis[505][505][5];

struct point{
    int x1,y1;//两个点,x1,y1永远在x2,y2前
    int x2,y2;
    int t;
    point(int a=0,int b=0,int c=0,int d=0,int f=0){
        x1=a;
        y1=b;
        x2=c;
        y2=d;
        t=f;
    }
};

//检查该状态是否可以去到
bool check(point tp){
    
    //如果是竖直向上
    if(tp.x1==tp.x2&&tp.y1==tp.y2){
        //判断是否是#或E
        if(maze[tp.x1][tp.y1]=='#'||maze[tp.x1][tp.y1]=='E')
            return false;
    }
    else{
        if(maze[tp.x1][tp.y1]=='#'||maze[tp.x2][tp.y2]=='#')
            return false;
    }

    //看该状态是否来过
    if(tp.x2==tp.x1&&tp.y1==tp.y2){
        if(vis[tp.x1][tp.y1][0])
            return false;
    }
    else{
        if(tp.x2-tp.x1!=0){
            if(vis[tp.x1][tp.y1][4]||vis[tp.x2][tp.y2][2])
                return false;

        }
        else{
            if(vis[tp.x1][tp.y1][3]||vis[tp.x2][tp.y2][1])
                return false;
        }
    }

    return true;

}


int bfs(point start){

    queue<point> que;
    que.push(start);


    //标记初始状态
    if(start.x2==start.x1&&start.y1==start.y2)
        vis[start.x1][start.y1][0]=1;
    else{
        if(start.x2-start.x1!=0){
            vis[start.x1][start.y1][4]=1;
            vis[start.x2][start.y2][2]=1;
        }
        else{
            vis[start.x1][start.y1][3]=1;
            vis[start.x2][start.y2][1]=1;
        }
    }


    while(!que.empty()){
        point tp=que.front();
        que.pop();

        //保证x1,y1比x2y2小
        if(tp.x2<tp.x1){
            swap(tp.x1,tp.x2);
            swap(tp.y1,tp.y2);
        }

        if(tp.y2<tp.y1){
            swap(tp.x1,tp.x2);
            swap(tp.y1,tp.y2);
        }

        //如果到了终点
        if(tp.x1==tp.x2&&tp.y1==tp.y2){
            if(maze[tp.x1][tp.y1]=='O')
                return tp.t;
        }


        //如果是竖直向上,可以向四个方向倒下
        if(tp.x1==tp.x2&&tp.y1==tp.y2){
            
            point temp=tp;
            temp.x1++;
            temp.x2+=2;
            temp.t++;

            if(check(temp)){
                que.push(temp);
                vis[temp.x1][temp.y1][4]=1;
                vis[temp.x2][temp.y2][2]=1;
            }

            temp=tp;
            temp.x1-=2;
            temp.x2--;
            temp.t++;
            if(check(temp)){
                que.push(temp);
                vis[temp.x1][temp.y1][4]=1;
                vis[temp.x2][temp.y2][2]=1;
            }

            temp=tp;
            temp.y1++;
            temp.y2+=2;
            temp.t++;
            if(check(temp)){
                que.push(temp);
                vis[temp.x1][temp.y1][3]=1;
                vis[temp.x2][temp.y2][1]=1;
            }

            temp=tp;
            temp.y1-=2;
            temp.y2--;
            temp.t++;
            if(check(temp)){
                que.push(temp);
                vis[temp.x1][temp.y1][3]=1;
                vis[temp.x2][temp.y2][1]=1;
            }

        }
        else{

            //如果是竖着放,可以向前向后变成直立,或者向左向右滚
            if(tp.x2-tp.x1!=0){

                point temp=tp;
                temp.x1--;
                temp.x2=temp.x1;
                temp.t++;
                if(check(temp)){
                    que.push(temp);
                    vis[temp.x1][temp.y1][0]=1;
                }

                temp=tp;
                temp.x2++;
                temp.x1=temp.x2;
                temp.t++;
                if(check(temp)){
                    que.push(temp);
                    vis[temp.x1][temp.y1][0]=1;
                }


                temp=tp;
                temp.y1--;
                temp.y2--;
                temp.t++;
                if(check(temp)){
                    que.push(temp);
                    vis[temp.x1][temp.y1][4]=1;
                    vis[temp.x2][temp.y2][2]=1;
                }

                temp=tp;
                temp.y1++;
                temp.y2++;
                temp.t++;
                if(check(temp)){
                    que.push(temp);
                    vis[temp.x1][temp.y1][4]=1;
                    vis[temp.x2][temp.y2][2]=1;
                }

            }
            else{

                //如果是横着放,可以向左向右变成直立,或者向前向后滚
                point temp=tp;
                temp.y1--;
                temp.y2=temp.y1;
                temp.t++;
                if(check(temp)){
                    que.push(temp);
                    vis[temp.x1][temp.y1][0]=1;
                }

                temp=tp;
                temp.y2++;
                temp.y1=temp.y2;
                temp.t++;
                if(check(temp)){
                    que.push(temp);
                    vis[temp.x1][temp.y1][0]=1;
                }


                temp=tp;
                temp.x1--;
                temp.x2--;
                temp.t++;
                if(check(temp)){
                    que.push(temp);
                    vis[temp.x1][temp.y1][3]=1;
                    vis[temp.x2][temp.y2][1]=1;
                }

                temp=tp;
                temp.x1++;
                temp.x2++;
                temp.t++;
                if(check(temp)){
                    que.push(temp);
                    vis[temp.x1][temp.y1][3]=1;
                    vis[temp.x2][temp.y2][1]=1;
                }

            }

        }

    }


    return -1;
}


int main()
{
    char str[505];

    while(scanf("%d%d",&N,&M)&&N&&M){

        memset(maze,0,sizeof(maze));
        memset(vis,0,sizeof(vis));


        for(int i=0;i<=N+1;i++){
            maze[i][0]='#';
            maze[i][M+1]='#';
        }

        for(int i=0;i<=M+1;i++){
            maze[0][i]='#';
            maze[N+1][i]='#';
        }

        //开始点,要判断是否只有一个点
        point temp;
        int num=1;

        for(int i=1;i<=N;i++){
            //要用C的输入,cin会超时
            scanf("%s" ,str);

            for(int j = 1 ;j <= M ;j ++){
                maze[i][j]=str[j-1];

                if(maze[i][j]=='X'){
                    if(num==1){
                        temp.x1=i;
                        temp.y1=j;
                        num++;
                    }
                    else{
                        temp.x2=i;
                        temp.y2=j;
                        num++;
                    }
                    maze[i][j]='.';
                }

            }
        }


        //如果只有一个X
        if(num==2){
            temp.x2=temp.x1;
            temp.y2=temp.y1;
        }
        temp.t=0;

        int ans=bfs(temp);
        if(ans==-1)
            cout<<"Impossible"<<endl;
        else
            cout<<ans<<endl;

        
    }

    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值