poj 1475 Pushing Boxes (推箱子游戏 三维数组判重)

Pushing Boxes
Time Limit: 2000MS   Memory Limit: 131072K
Total Submissions: 3975   Accepted: 1403   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? 
poj 1475 Pushing Boxes (推箱子游戏 三维数组判重)_第1张图片

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 具体就不说了 这几天任务比较重
 
 
 
 
感想:poj上这题不报PE  只报WA  WA的朋友试试 注意最后得加换行  我就被他坑了 ╮(╯▽╰)╭
ps:这题必须要用三维数组判重  不然有些数据是过不了的  具体见我贴的一组数据 不过在poj上是可以AC的  因为poj数据太水……
 
 
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#define maxn 25
using namespace std;

int n,m;
int bsx,bsy,ssx,ssy,tex,tey;
int stsx,stsy,esx,esy;
int mp[maxn][maxn];
int boxvis[maxn][maxn][4];    // 箱子要采用3维数组存状态  第三维-得到箱子此时的状态的方向
int peovis[maxn][maxn];
int dx[4]={-1,1,0,0};         //  up down left right
int dy[4]={0,0,-1,1};
char dir1[]="nswe";
char dir2[]="NSWE";
char s[maxn];
string ans,zans;
struct Tnode
{
    int bx,by;
    int sx,sy;
    int cnt;
    string path;
}cur,now;
struct Node
{
    int zx,zy;
    string zpath;
}zcur,znow;
queue<Tnode> q;
queue<Node> q1;

bool sbfs()                             // 对人bfs
{ 
    int i,j;
    int xx,yy,nxx,nyy;
    string temps1,temps2;
    while(!q1.empty()) q1.pop();
    memset(peovis,0,sizeof(peovis));
    zcur.zx=stsx;
    zcur.zy=stsy;
    zcur.zpath="";
    q1.push(zcur);
    peovis[stsx][stsy]=1;
    while(!q1.empty())
    {
        znow=q1.front();
        xx=znow.zx;
        yy=znow.zy;
        temps1=znow.zpath;
        if(xx==esx&&yy==esy)
        {
            zans=temps1;
            return true;
        }
        for(i=0;i<4;i++)
        {
            nxx=xx+dx[i];
            nyy=yy+dy[i];
            if(!mp[nxx][nyy]&&!peovis[nxx][nyy])
            {
                peovis[nxx][nyy]=1;
                zcur.zx=nxx;
                zcur.zy=nyy;
                temps2=temps1+dir1[i];
                zcur.zpath=temps2;
                q1.push(zcur);
            }
        }
        q1.pop();
    }
    return false;
}
bool isok(int u,int v,int ii)
{
    if(mp[u][v]||boxvis[u][v][ii]) return false;
    if(sbfs()) return true;
    return false;
}
bool boxbfs()                             // 对箱子bfs
{
    int i,j,x,y,x1,y1,nx,ny,nx1,ny1;
    string bs1,bs2,ps;
    while(!q.empty()) q.pop();
    memset(boxvis,0,sizeof(boxvis));
    cur.bx=bsx;
    cur.by=bsy;
    cur.sx=ssx;
    cur.sy=ssy;
    cur.path="";
    q.push(cur);
    while(!q.empty())
    {
        now=q.front();
        x=now.bx;
        y=now.by;
        x1=now.sx;
        y1=now.sy;
        ps=now.path;
 //       printf("x:%d y:%d x1:%d y1:%d\n",x,y,x1,y1);
 //       cout<<ps<<endl;
        if(x==tex&&y==tey)
        {
            ans=ps;
            return true;
        }
        for(i=0;i<4;i++)
        {
            nx=x+dx[i];
            ny=y+dy[i];
            stsx=x1;
            stsy=y1;
            esx=x-dx[i];
            esy=y-dy[i];
            mp[x][y]=1;
            if(isok(nx,ny,i))
            {
                boxvis[nx][ny][i]=1;
                bs1=ps+zans+dir2[i];
                cur.bx=nx;
                cur.by=ny;
                cur.sx=x;
                cur.sy=y;
                cur.path=bs1;
                q.push(cur);
            }
            mp[x][y]=0;
        }
        q.pop();
    }
    return false;
}
int main()
{
    int i,j,xxc=0;
    while(scanf("%d%d",&n,&m),n||m)
    {
        xxc++;
        memset(mp,1,sizeof(mp));
        for(i=1;i<=n;i++)
        {
            scanf("%s",s);
            for(j=1;j<=m;j++)
            {
                if(s[j-1]=='.') mp[i][j]=0;
                else if(s[j-1]=='B')
                {
                    mp[i][j]=0;
                    bsx=i;
                    bsy=j;
                }
                else if(s[j-1]=='T')
                {
                    mp[i][j]=0;
                    tex=i;
                    tey=j;
                }
                else if(s[j-1]=='S')
                {
                    mp[i][j]=0;
                    ssx=i;
                    ssy=j;
                }
                else if(s[j-1]=='#')
                {
                    mp[i][j]=1;
                }
            }
        }
        printf("Maze #%d\n",xxc);
        if(boxbfs()) cout<<ans<<endl;
        else printf("Impossible.\n");
        printf("\n");
    }
    return 0;
}
// 贴一组数据 这组数据网上很多AC代码 都不能过
/*
8 9
#########
#......T#
#.S.....#
##B######
#.......#
#.......#
#.......#
#########
*/


 
 
 
 
 

你可能感兴趣的:(poj 1475 Pushing Boxes (推箱子游戏 三维数组判重))