• POJ 1475 Pushing Boxes


    Pushing Boxes
    Time Limit: 2000MS   Memory Limit: 131072K
    Total Submissions: 3943   Accepted: 1394   Special Judge

    Description

    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
    

    Source

     
     
     
    题意:推箱子。
    算法:双重bfs:箱子到达目的地,人到达箱子的前一个位置
    注意:在箱子能够达到下一个位置的同时,要保证箱子的前一个位置是可以到达的,不能够是障碍物,否则人无法去推
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<queue>
    #include<string>
    
    using namespace std;
    
    const int N=30;
    
    char map[N][N];
    bool visP[N][N],visB[N][N];
    int R,C;
    
    int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
    char pushes[4]={'E','W','S','N'};
    char walks[4]={'e','w','s','n'};
    string path;
    
    struct node{
        int br,bc;  // 箱子的位置
        int pr,pc;  // 人的位置
        string ans; // 当前操作
    };
    
    bool InMap(int x,int y){
        if(x>=1 && x<=R && y>=1 && y<=C)
            return 1;
        return 0;
    }
    
    // 人到箱子     // (sr,sc):人的位置   // (er,ec):箱子的前一个位置  // (br,bc):箱子的位置  // ans:路径
    bool BFS2(int sr,int sc,int er,int ec,int br,int bc,string &ans){
        memset(visP,0,sizeof(visP));
        queue<node> q;
        while(!q.empty())
            q.pop();
        node cur,tmp;
        cur.pr=sr; cur.pc=sc;
        cur.ans="";
        q.push(cur);
        visP[br][bc]=1;     // 此处保证不从箱子上走过
        while(!q.empty()){
            cur=q.front();
            q.pop();
            if(cur.pr==er && cur.pc==ec){   // 如果能够走到箱子,立即返回
                ans=cur.ans;
                return 1;
            }
            if(visP[cur.pr][cur.pc])
                continue;
            visP[cur.pr][cur.pc]=1;
            for(int i=0;i<4;i++){
                tmp.pr=cur.pr+dir[i][0];
                tmp.pc=cur.pc+dir[i][1];
                if(InMap(tmp.pr,tmp.pc) && !visP[tmp.pr][tmp.pc] && map[tmp.pr][tmp.pc]!='#'){
                    tmp.ans=cur.ans+walks[i];
                    q.push(tmp);
                }
            }
        }
        return 0;
    }
    
    // 箱子到终点   // (sr,sc):人的位置   // (br,bc):箱子的位置
    bool BFS1(int sr,int sc,int br,int bc){
        memset(visB,0,sizeof(visB));
        queue<node> q;
        while(!q.empty())
            q.pop();
        node cur,tmp;
        cur.pr=sr; cur.pc=sc;
        cur.br=br; cur.bc=bc;
        cur.ans="";
        q.push(cur);
        while(!q.empty()){
            cur=q.front();
            q.pop();
            if(visB[cur.br][cur.bc])
                continue;
            visB[cur.br][cur.bc]=1;
            if(map[cur.br][cur.bc]=='T'){
                path=cur.ans;
                return 1;
            }
            for(int i=0;i<4;i++){
                // 箱子的下一个位置
                int nextr=cur.br+dir[i][0];
                int nextc=cur.bc+dir[i][1];
                // 箱子的前一个位置
                int backr=cur.br-dir[i][0];
                int backc=cur.bc-dir[i][1];
                string ans="";
                if(InMap(backr,backc) && InMap(nextr,nextc) && map[backr][backc]!='#' && map[nextr][nextc]!='#' && !visB[nextr][nextc]){
                    if(BFS2(cur.pr,cur.pc,backr,backc,cur.br,cur.bc,ans)){
                        tmp.pr=cur.br;
                        tmp.pc=cur.bc;
                        tmp.br=nextr;
                        tmp.bc=nextc;
                        tmp.ans=cur.ans+ans+pushes[i];
                        q.push(tmp);
                    }
                }
            }
        }
        return 0;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int sr,sc;  // 起始点
        int br,bc;  // 箱子的位置
        int cases=0;
        while(~scanf("%d%d",&R,&C)){
            if(R==0 && C==0)
                break;
            int i,j;
            for(i=1;i<=R;i++)
                for(j=1;j<=C;j++){
                    cin>>map[i][j];
                    if(map[i][j]=='S'){
                        sr=i;sc=j;
                    }
                    if(map[i][j]=='B'){
                        br=i;bc=j;
                    }
                }
            path="";
            if(BFS1(sr,sc,br,bc))
                cout<<"Maze #"<<++cases<<endl<<path<<endl<<endl;
            else
                cout<<"Maze #"<<++cases<<endl<<"Impossible."<<endl<<endl;
        }
        return 0;
    }
  • 相关阅读:
    第二次冲刺每日站立会议10(完结)
    第二次冲刺每日站立会议09
    第二次冲刺每日站立会议08
    找bug
    测试计划
    博客园的意见与建议
    第二次每日站立会议07
    个人总结
    学习进度条(第十六周)
    梦断代码阅读笔记03
  • 原文地址:https://www.cnblogs.com/jackge/p/3070439.html
Copyright © 2020-2023  润新知