集训队作业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上提交(样例过了就是过了!!!)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
´问题描述: 码头仓库是划分为n×m个格子的矩形阵列。有公共边的格子是相邻格子。当前仓库中 有的格子是空闲的;有的格子则已经堆放了沉重的货物。由于堆放的货物很重,单凭仓库管 理员的力量是无法移动的。仓库管理员有一项任务,要将一个小箱子到指定的格子上去。 管理员可以在仓库中移动,但不能跨过已经堆放了货物的格子。管理员站在与箱子相对的空 闲格子上时,可以做一次动,把箱子到另一相邻的空闲格子。箱时只能向管理员的对 面方向。由于要动的箱子很重,仓库管理员想尽量减少箱子的次数。 ´编程任务: 对于给定的仓库布局,以及仓库管理员在仓库中的位置和箱子的开始位置和目标位置, 设计一个解箱子问题的分支限界法, 计算出仓库管理员将箱子从开始位置到目标位置所 需的最少动次数。 ´数据输入: 由文件input.txt提供输入数据。输入文件第 1 行有 2个正整数 n和 m(1<=n,m<=100) , 表示仓库是n×m个格子的矩形阵列。接下来有 n行,每行有 m个字符,表示格子的状态。 S 表示格子上放了不可移动的沉重货物; w 表示格子空闲; M 表示仓库管理员的初始位置; P 表示箱子的初始位置; K 表示箱子的目标位置。 ´结果输出: 将计算出的最少动次数输出到文件 output.txt。如果仓库管理员无法将箱子从开始位 置到目标位置则输出“No solution!” 。 输入文件示例 输出文件示例 input.txt output.txt
CSDN IT狂飙上传的代码均可运行,功能ok的情况下才上传的,直接替换数据即可使用,小白也能轻松上手 【资源说明】 基于MATLAB实现的有限差分法实验报告用MATLAB中的有限差分法计算槽内电位;对比解析法和数值法的异同点;选取一点,绘制收敛曲线;总的三维电位图+使用说明文档 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2020b;若运行有误,根据提示GPT修改;若不会,私信博主(问题描述要详细); 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可后台私信博主; 4.1 期刊或参考文献复现 4.2 Matlab程序定制 4.3 科研合作 功率谱估计: 故障诊断分析: 雷达通信:雷达LFM、MIMO、成像、定位、干扰、检测、信号分析、脉冲压缩 滤波估计:SOC估计 目标定位:WSN定位、滤波跟踪、目标定位 生物电信号:肌电信号EMG、脑电信号EEG、心电信号ECG 通信系统:DOA估计、编码译码、变分模态分解、管道泄漏、滤波器、数字信号处理+传输+分析+去噪、数字信号调制、误码率、信号估计、DTMF、信号检测识别融合、LEACH协议、信号检测、水声通信 5、欢迎下载,沟通交流,互相学习,共同进步!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值