您的位置:首页 > 其它

POJ 1475 Pushing Boxes (双重BFS/推箱子游戏)

2013-07-09 02:22 302 查看
Pushing Boxes

Time Limit: 2000MSMemory Limit: 131072K
Total Submissions: 3975Accepted: 1403Special 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


题目分析:

1、保证箱子的前一位置和后一位置不为障碍物,如果前一位置为障碍物,人不能达到;后一位置为障碍物,箱子无法推动

2、题目的目的是:箱子到达目的地,人到达箱子的前一位置。

3、注意cin>>可以吃掉换行,如果用scanf()读入,要注意用gets()吃掉换行。

感想:

第一次用queue写bfs(),感觉还不错~



代码:

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#define N 30

using namespace std;

int dx[4]={-1,1,0,0};      //分别对应n\s\w\e
int dy[4]={0,0,-1,1};

struct node
{
int pr,pc;      //人的位置
int br,bc;      //箱子的位置
string ans;     //当前操作
};

char pushes[4]={'N','S','W','E'};    //方向与坐标变动一定要一一对应
char walks[4]={'n','s','w','e'};
string path;

int r,c;
int visp

;
int visb

;
char maps

;

bool isinmaps(int x,int y)
{
if(x>=1 && x<=r && y>=1 && y<=c)
return true;
else
return false;
}

//判断人是否能到箱子前一格   //(sr,sc):人的位置   //(er,ec):箱子的前一个位置   //(bbr,bbc):箱子的位置   //ans:路径记录
int bfs(int sr,int sc,int er,int ec,int bbr,int bbc,string &ans1)
{
memset(visp,0,sizeof(visp));
node cur,temp;
queue<node> q;              //用队列存位置
while(!q.empty())           //队列使用之前清空
q.pop();
cur.pr=sr;                 //队列初始化
cur.pc=sc;
cur.ans="";
q.push(cur);
visp[bbr][bbc]=1;        //保证人不从箱子上走过
while(!q.empty())          //如果队列不空,就继续广搜
{
cur=q.front();         //取队头元素
q.pop();               //对头出栈,表示已检查其周围位置
if(cur.pr==er&&cur.pc==ec)      //如果能够走到箱子前一格,立即返回
{
ans1=cur.ans;
return 1;
}
if(visp[cur.pr][cur.pc])
continue;
visp[cur.pr][cur.pc]=1;
for(int i=0;i<4;i++)         //向四个方向搜索
{
temp.pr=cur.pr+dx[i];
temp.pc=cur.pc+dy[i];
if(isinmaps(temp.pr,temp.pc)&&!visp[temp.pr][temp.pc]&&maps[temp.pr][temp.pc]!='#')
{
temp.ans=cur.ans+walks[i];
q.push(temp);                //如果当前位置可走则入队
}
}
}
return 0;
}

// 箱子到终点   // (sr,sc):人的位置   // (br,bc):箱子的位置
int bfs1(int sr,int sc,int bbr,int bbc)
{
memset(visb,0,sizeof(visb));
node cur1,temp1;
queue<node> q1;
while(!q1.empty())
q1.pop();
cur1.pr=sr;
cur1.pc=sc;
cur1.br=bbr;
cur1.bc=bbc;
cur1.ans="";
q1.push(cur1);
while(!q1.empty())
{
cur1=q1.front();
q1.pop();
if(visb[cur1.br][cur1.bc])
continue;
visb[cur1.br][cur1.bc]=1;
if(maps[cur1.br][cur1.bc]=='T')
{
path=cur1.ans;
return 1;
}
for(int i=0;i<4;i++)
{
//箱子的前一个位置
int nextr=cur1.br+dx[i];
int nextc=cur1.bc+dy[i];

//箱子的下一个位置
int backr=cur1.br-dx[i];
int backc=cur1.bc-dy[i];
string ans="";
if(isinmaps(nextr,nextc) && isinmaps(backr,backc) && maps[nextr][nextc]!='#' && maps[backr][backc]!='#' && !visb[nextr][nextc])
{
if(bfs(cur1.pr,cur1.pc,backr,backc,cur1.br,cur1.bc,ans))
{
temp1.pr=cur1.br;           //更新箱子和人的位置
temp1.pc=cur1.bc;
temp1.br=nextr;
temp1.bc=nextc;
temp1.ans=cur1.ans+ans+pushes[i];       //路径为上次的+人到箱子+人推箱子前进的步数
q1.push(temp1);
}
}
}
}
return 0;
}

int main()
{
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>>maps[i][j];
if(maps[i][j]=='S')
{
sr=i;sc=j;
}
if(maps[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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: