zoukankan      html  css  js  c++  java
  • (poj 1475) Pushing Boxes

    Imagine you are standing inside a two-dimensional maze composed of square cells which may or may not be filled with rock. You can move north, south, east or west one cell at a step. These moves are called walks. 
    One of the empty cells contains a box which can be moved to an adjacent free cell by standing next to the box and then moving in the direction of the box. Such a move is called a push. The box cannot be moved in any other way than by pushing, which means that if you push it into a corner you can never get it out of the corner again. 


    One of the empty cells is marked as the target cell. Your job is to bring the box to the target cell by a sequence of walks and pushes. As the box is very heavy, you would like to minimize the number of pushes. Can you write a program that will work out the best such sequence? 


    Input


    The input contains the descriptions of several mazes. Each maze description starts with a line containing two integers r and c (both <= 20) representing the number of rows and columns of the maze. 


    Following this are r lines each containing c characters. Each character describes one cell of the maze. A cell full of rock is indicated by a `#' and an empty cell is represented by a `.'. Your starting position is symbolized by `S', the starting position of the box by `B' and the target cell by `T'. 


    Input is terminated by two zeroes for r and c. 
    Output


    For each maze in the input, first print the number of the maze, as shown in the sample output. Then, if it is impossible to bring the box to the target cell, print ``Impossible.''. 


    Otherwise, output a sequence that minimizes the number of pushes. If there is more than one such sequence, choose the one that minimizes the number of total moves (walks and pushes). If there is still more than one such sequence, any one is acceptable. 


    Print the sequence as a string of the characters N, S, E, W, n, s, e and w where uppercase letters stand for pushes, lowercase letters stand for walks and the different letters stand for the directions north, south, east and west. 


    Output a single blank line after each test case. 
    Sample Input


    1 7
    SB....T
    1 7
    SB..#.T
    7 11
    ###########
    #T##......#
    #.#.#..####
    #....B....#
    #.######..#
    #.....S...#
    ###########
    8 4
    ....
    .##.
    .#..
    .#..
    .#.B
    .##S
    ....
    ###T
    0 0
    Sample Output


    Maze #1
    EEEEE


    Maze #2
    Impossible.


    Maze #3
    eennwwWWWWeeeeeesswwwwwwwnNN


    Maze #4

    swwwnnnnnneeesssSSS

    题目分析:

    单纯的bfs无法处理“推” 和 "走“两个过程,于是需要用到双重bfs。先bfs”推“,在没推一次时bfs”走".

    # include<iostream>
    # include<cstring>
    # include<string>
    using namespace std;
    struct node
    {
        int sx,sy,bx,by;
        string ans;
    } lxk[2][1000005];
    int r,c,via[][2]={-1,0,1,0,0,-1,0,1};//注意题目要求的是n、s、w、e的顺序,因为这个wa了一次
    char op[]={'n','s','w','e'};
    char box[25][25];
    char up(char c)
    {
        return (c-'a'+'A');
    }
    bool bfs(int tx,int ty,node &cc)//对走的过程bfs,搜索从推上一步到当前推箱子的前一格
    {
        if (tx<=0||tx>r||ty<=0||ty>c||box[tx][ty]=='#') return false;
        bool mark[25][25];
        memset(mark,false,sizeof(mark));
        lxk[1][1]=cc;
        int st,en;
        st=1; en=1;
        node a,b;
        bool p=false;
        while (st<=en)
        {
            a=lxk[1][st];
            if (a.sx==tx&&a.sy==ty)
            {
                p=true;
                cc=a;
                break;
            }
            for (int i=0;i<4;i++)
            {
                int x,y;
                x=a.sx+via[i][0]; y=a.sy+via[i][1];
                if (x>0&&x<=r&&y>0&&y<=c&&!(x==a.bx&&y==a.by)&&!mark[x][y]&&box[x][y]!='#')
                {
                    b=a;
                    b.sx=x; b.sy=y;
                    b.ans=b.ans+op[i];
                    mark[x][y]=true;
                    en++;
                    lxk[1][en]=b;
                }
            }
            st++;
        }
        return p;
    }
    int main()
    {
        int i,j,tx,ty,ca=1;
        while (~scanf("%d%d",&r,&c))
        {
            if (!r||!c) break;
            for (i=1;i<=r;i++)
            {
                getchar();
                for (j=1;j<=c;j++)
                {
                    scanf("%c",&box[i][j]);
                    if (box[i][j]=='T')
                    {
                        tx=i; ty=j;
                        box[i][j]='.';
                    }
                    if (box[i][j]=='S')
                    {
                        lxk[0][1].sx=i; lxk[0][1].sy=j;
                        box[i][j]='.';
                    }
                    if (box[i][j]=='B')
                    {
                        lxk[0][1].bx=i; lxk[0][1].by=j;
                        box[i][j]='.';
                    }
                }
            }
            lxk[0][1].ans="";
            int st,en;
            st=1; en=1;
            node a,b;
            bool p=false;
            bool mark[25][25][4];
            memset(mark,false,sizeof(mark));
            string ans;
            printf("Maze #%d
    ",ca++);
            while (st<=en)//对推的过程bfs
            {
                a=lxk[0][st];
                if (a.bx==tx&&a.by==ty)
                {
                    p=true;
                    ans=a.ans;
                    break;
                }
                for (i=0;i<4;i++)
                {
                    int x,y;
                    x=a.bx+via[i][0]; y=a.by+via[i][1];  
                    if (x>0&&x<=r&&y>0&&y<=c&&!mark[x][y][i]&&box[x][y]!='#')
                    {
                        b=a;
                        if (bfs(x-2*via[i][0],y-2*via[i][1],b))
                        {
                            b.sx=b.bx; b.sy=b.by;
                            b.bx=x; b.by=y;
                            b.ans=b.ans+up(op[i]);
                            mark[x][y][i]=true;
                            en++;
                            lxk[0][en]=b;
                        }
                    }
                }
                st++;
            }
            if (p) printf("%s
    
    ",ans.c_str());
            else printf("Impossible.
    
    ");
        }
        return 0;
    }
    


  • 相关阅读:
    优秀的云架构师需要学什么技能
    dkh人力资源大数据解决方案整体架构
    大数据hadoop与spark的区别
    hadoop技术入门学习之发行版选择
    大数据开发基础知识需要掌握哪些
    智慧人社政务云平台建设方案架构案例介绍
    [项目机会]citrix 虚拟桌面对于java等高CPU占用率如何解决
    [办公自动化]无法使用江南天安usbkey 无法使用视频网站
    [学习笔记]从0到1
    [办公自动化]目录修改以及插入分页符后行间距自动变宽
  • 原文地址:https://www.cnblogs.com/riskyer/p/3239083.html
Copyright © 2011-2022 走看看