poj 1475 Pushing Boxes

//
//  main.cpp
//  poj1475
//
//  Created by Charles on 2017/7/12.
//  Copyright © 2017年 Charles. All rights reserved.
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#include <string>
using namespace std;
int r,c;
char map[21][21];
bool visb[21][21][4],vism[21][21];
int d[4][2]={1,0,-1,0,0,1,0,-1};
char man[4]={'s','n','e','w'},box[4]={'S','N','E','W'};

struct node {
    int x,y,px,py;
    string ans;
}t,b,s;
bool check(int x,int y){
    if(x<0 ||y<0 || x>=r ||y>=c || map[x][y]=='#') return 0;
    return 1;
}

string tmp;
bool bfs(node a,node b){
    tmp="";
    node st;
    st.x=a.px;
    st.y=a.py;
    st.ans="";
    queue<node> q;
    while(!q.empty()) q.pop();
    q.push(st);
    memset(vism, 0, sizeof(vism));
    node cur,next;
    while(!q.empty()){
        cur=q.front();  q.pop();
        if(cur.x==a.x && cur.y== a.y) {
            tmp=cur.ans;
            return 1;
        }
        for(int i=0;i<4;++i){
            next=cur;
            next.x+=d[i][0];
            next.y+=d[i][1];
            if(vism[next.x][next.y]  || !check(next.x, next.y)) continue;
            if(next.x==b.x && next.y==b.y) continue;
            vism[next.x][next.y]=1;
            next.ans=cur.ans+man[i];
            q.push(next);
        }
    }
    return 0;
}


string bfsb(){
    node st;
    st.x=b.x;   st.y=b.y;
    st.px=s.x;  st.py=s.y;
    st.ans="";
    queue<node> q;
    while(!q.empty()) q.pop();
    q.push(st);
    node cur,pre,next;
    memset(visb, 0, sizeof(visb));
    while(!q.empty()){
        cur=q.front();  q.pop();
        if(map[cur.x][cur.y]=='T')
            return cur.ans;
        for(int i=0;i<4;++i){
            next=cur;
            next.x+=d[i][0];
            next.y+=d[i][1];
            if(visb[next.x][next.y][i] || !check(next.x, next.y)) continue;
            pre=cur;
            switch (i) {
                case 0:
                    pre.x=cur.x-1;
                    break;
                case 1:
                    pre.x=cur.x+1;
                    break;
                case 2:
                    pre.y=cur.y-1;
                    break;
                case 3:
                    pre.y=cur.y+1;
                    break;
                default:
                    break;
            }
            if(!bfs(pre,cur)) continue;
            visb[next.x][next.y][i]=1;
            next.ans=cur.ans+tmp;
            next.ans+=box[i];
            next.px=cur.x;
            next.py=cur.y;
            q.push(next);
        }
        
    }
    return "Impossible.";
}
int main(){
    int cs=1;
    while(cin>>r>>c &&r+c){
        for(int i=0;i<r;++i) cin>>map[i];
        for(int i=0;i<r;++i){
            for(int j=0;j<c;++j){
                if(map[i][j]=='T'){
                    t.x=i;
                    t.y=j;
                }
                else if(map[i][j]=='B'){
                    b.x=i;
                    b.y=j;
                }
                else if(map[i][j]=='S'){
                    s.x=i;
                    s.y=j;
                }
            }
        }
        cout<<"Maze #"<<cs++<<endl;
        cout<<bfsb()<<endl<<endl;
    }
    
    return 0;
}

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

题意:
推箱子的游戏大家都玩过!,本题就是那个操作;
思路:
先bfs1箱子的路径,每走一步用bfs2判断人的路径(看是否能走到箱子的上一个位置,如果能,才进行下一次扩展)。即双bfs;
反思:纯bfs的方法效率终究不如A*,1400MS险些超时,下次补充A*的做法。
AC代码(1400MS):

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值