1. 程式人生 > >集訓隊作業2--poj推箱子1475

集訓隊作業2--poj推箱子1475

Pushing Boxes

Time Limit: 2000MS Memory Limit: 131072K
Total Submissions: 7212 Accepted: 2473 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

題目大意:相信大家都玩過推箱子這個遊戲吧,現在題目要求用最少的步數去移動箱子,並且人移動的步數不計算在箱子移動的步數之內,要求實現最小步數把箱子推入終點,並且打印出來箱子和人移動的路徑,如果箱子無法達到終點那麼就輸出impossible

這題是組內對抗賽的C題,可以看得出要用Bfs,我們的想法是先用BFS找到箱子的路徑,然後根據箱子的路徑來BFS人移動的路徑,看看人是否可以到達箱子的上一個位置,如果不能到達就說明箱子不能夠到達終點。那麼該如何實現這個思路呢?

首先來寫一個人對於箱子的BFS

bool BFS_Findman(int s,int e,int aa,int bb,int ss,int ee)
{
	queue<man> MM;
	if(s<0||s>m||e<0||e>n||mapp[s][e]=='#')	return false;
	mmst.x=ss;mmst.y=ee;mmst.ans="";
	memset(vis,0,sizeof(vis));
	vis[aa][bb]=1;
	vis[ss][ee]=1;
	MM.push(mmst);
	while(!MM.empty())
	{
		mmst=MM.front();
		MM.pop();
		if(mmst.x==s&&mmst.y==e)
		{
			return true;
		}
		for(int i=0;i<4;i++)
		{
			mmed.x=mmst.x+dir[i][0];
			mmed.y=mmst.y+dir[i][1];
			if(mmed.x>0&&mmed.x<=m&&mmed.y>0&&mmed.y<=n&&!vis[mmed.x][mmed.y]&&mapp[mmed.x][mmed.y]!='#')
			{
				mmed.ans=mmst.ans+ddir[i];
				vis[mmed.x][mmed.y]=1;
				MM.push(mmed);
			}
		}
	}
	return false;
}

這是判斷人是否可以到達箱子的周圍並且推動箱子向著終點走去

然後我們在寫一個箱子路徑的BFS

bool BFS_Findbox()
{
	queue<box> BB;
	bbst.x=boxx;bbst.y=boxy;bbst.ans="";
	bbst.mani=manx;bbst.manj=many;
	BB.push(bbst);
	while(!BB.empty())
	{
		bbst=BB.front();
		BB.pop();
		if(bbst.x==tarx&&bbst.y==tary)
		{
			return true;
		}
		for(int i=0;i<4;i++)
		{
			bbed.x=bbst.x+dir[i][0];
			bbed.y=bbst.y+dir[i][1];
			if(bbed.x>0&&bbed.y>0&&bbed.x<=m&&bbed.y<=n&&!mark[bbed.x][bbed.y][i]&&mapp[bbed.x][bbed.y]!='#')
			{
				if(BFS_Findman(bbed.x-2*dir[i][0],bbed.y-2*dir[i][1],bbst.x,bbst.y,bbst.mani,bbst.manj))
				{
					bbed.mani=bbst.x;
					bbed.manj=bbst.y;
					bbed.ans=bbst.ans+mmst.ans+(ddir[i]);
					mark[bbed.x][bbed.y][i]=1;
					BB.push(bbed);
				}
			}
		}
	}
	return false;
}

找到箱子的路徑,其中在箱子的結構體中有一個string型別的ans來儲存箱子的路徑

下面是整個的程式碼

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
int boxx,boxy;
int manx,many;
int tarx,tary;
int dir[][2]={-1,0,1,0,0,-1,0,1};
//這是n s w e 的順序
int m,n;
char ddir[]={'n','s','w','e'};
bool mark[25][25][4];
bool vis[25][25];
char mapp[25][25];
struct box{
	int x,y;
	int mani,manj;
	string ans;
}bbst,bbed;
struct man{
	int x,y;
	string ans;
}mmst,mmed;

char i2I(char i)
{
	return (i-'a'+'A');
}

bool BFS_Findman(int s,int e,int aa,int bb,int ss,int ee)
{
	queue<man> MM;
	if(s<0||s>m||e<0||e>n||mapp[s][e]=='#')	return false;
	mmst.x=ss;mmst.y=ee;mmst.ans="";
	memset(vis,0,sizeof(vis));
	vis[aa][bb]=1;
	vis[ss][ee]=1;
	MM.push(mmst);
	while(!MM.empty())
	{
		mmst=MM.front();
		MM.pop();
		if(mmst.x==s&&mmst.y==e)
		{
			return true;
		}
		for(int i=0;i<4;i++)
		{
			mmed.x=mmst.x+dir[i][0];
			mmed.y=mmst.y+dir[i][1];
			if(mmed.x>0&&mmed.x<=m&&mmed.y>0&&mmed.y<=n&&!vis[mmed.x][mmed.y]&&mapp[mmed.x][mmed.y]!='#')
			{
				mmed.ans=mmst.ans+ddir[i];
				vis[mmed.x][mmed.y]=1;
				MM.push(mmed);
			}
		}
	}
	return false;
}
bool BFS_Findbox()
{
	queue<box> BB;
	bbst.x=boxx;bbst.y=boxy;bbst.ans="";
	bbst.mani=manx;bbst.manj=many;
	BB.push(bbst);
	while(!BB.empty())
	{
		bbst=BB.front();
		BB.pop();
		if(bbst.x==tarx&&bbst.y==tary)
		{
			return true;
		}
		for(int i=0;i<4;i++)
		{
			bbed.x=bbst.x+dir[i][0];
			bbed.y=bbst.y+dir[i][1];
			if(bbed.x>0&&bbed.y>0&&bbed.x<=m&&bbed.y<=n&&!mark[bbed.x][bbed.y][i]&&mapp[bbed.x][bbed.y]!='#')
			{
				if(BFS_Findman(bbed.x-2*dir[i][0],bbed.y-2*dir[i][1],bbst.x,bbst.y,bbst.mani,bbst.manj))
				{
					bbed.mani=bbst.x;
					bbed.manj=bbst.y;
					bbed.ans=bbst.ans+mmst.ans+(ddir[i]);
					mark[bbed.x][bbed.y][i]=1;
					BB.push(bbed);
				}
			}
		}
	}
	return false;
}
int main()
{
	int T,k=1;
	while(scanf("%d%d",&m,&n)&&m+n)
	{
		memset(mark,0,sizeof(mark)); 
		memset(mapp,'#',sizeof(mapp));
		for(int i=1;i<=m;i++)
			for(int j=1;j<=n;j++)
			{
				cin>>mapp[i][j];
				if(mapp[i][j]=='S')
				{
					manx=i;many=j;
				}
				if(mapp[i][j]=='T')
				{
					tarx=i;tary=j;
				}
				if(mapp[i][j]=='B')
				{
					boxx=i;boxy=j;
				}
			}
		printf("Maze #%d\n",k++);
		if(BFS_Findbox()) printf("%s\n\n",bbst.ans.c_str());
		else printf("Impossible.\n\n");
	}
	return 0;
}

這個我只測試了本地的資料,並沒有交到poj上提交(樣例過了就是過了!!!)