列表

详情


NC51017. Pushing Boxes

描述

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?

输入描述

The input contains the descriptions of several mazes. Each maze description starts with a line containing two integers r and c () 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.

输出描述

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). 本题没有 Special Judge,多解时,先最小化箱子被推动的次数,再最小化人移动的步数。若仍有多条路线,则按照N、S、W、E的顺序优先选择箱子的移动方向(即先上下推,再左右推)。在此前提下,再按照n、s、w、e的顺序优先选择人的移动方向(即先上下动,再左右动)。
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.

示例1

输入:

1 7
SB....T
1 7
SB..#.T
7 11
###########
#T##......#
#.#.#..####
#....B....#
#.######..#
#.....S...#
###########
8 4
....
.##.
.#..
.#..
.#.B
.##S
....
###T
0 0

输出:

Maze #1
EEEEE

Maze #2
Impossible.

Maze #3
eennwwWWWWeeeeeesswwwwwwwnNN

Maze #4
swwwnnnnnneeesssSSS

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(g++ 7.5.0) 解法, 执行用时: 65ms, 内存消耗: 432K, 提交时间: 2022-11-14 16:42:16

//Author:XuHt
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 26, INF = 0x3f3f3f3f;
char A[4] = {'N','S','W','E'};
char a[4] = {'n','s','w','e'};
int r, c, num = 0;
int d[4][2]= {{-1,0},{1,0},{0,-1},{0,1}};
char s[N][N];
string tmp;
struct P {
	int x, y, px, py;
	string ans;
};

bool pd(int x, int y) {
	return x > 0 && x <= r && y > 0 && y <= c && s[x][y] != '#';
}

bool bfs2(P p1, P p2) {
	tmp = "";
	P st;
	st.x = p1.px;
	st.y = p1.py;
	st.ans = "";
	queue<P> q;
	q.push(st);
	bool v[N][N];
	memset(v, 0, sizeof(v));
	while (q.size()) {
		P now = q.front(), nxt;
		q.pop();
		if (now.x == p1.x && now.y == p1.y) {
			tmp = now.ans;
			return 1;
		}
		for (int i = 0; i < 4; i++) {
			nxt = now;
			nxt.x = now.x + d[i][0];
			nxt.y = now.y + d[i][1];
			if (!pd(nxt.x, nxt.y)) continue;
			if (nxt.x == p2.x && nxt.y == p2.y) continue;
			if (v[nxt.x][nxt.y]) continue;
			v[nxt.x][nxt.y] = 1;
			nxt.ans = now.ans + a[i];
			q.push(nxt);
		}
	}
	return 0;
}

string bfs1() {
	P st;
	st.x = st.y = st.px = st.py = -1;
	st.ans = "";
	for (int i = 1; i <= r && (st.x == -1 || st.px == -1); i++)
		for (int j = 1; j <= c && (st.x == -1 || st.px == -1); j++)
			if (s[i][j] == 'B') {
				st.x = i;
				st.y = j;
				s[i][j] = '.';
			} else if (s[i][j] == 'S') {
				st.px = i;
				st.py = j;
				s[i][j] = '.';
			}
	queue<P> q;
	q.push(st);
	bool v[N][N][4];
	memset(v, 0, sizeof(v));
	string ans = "Impossible.";
	unsigned int cntans = INF, cnt = INF;
	while (q.size()) {
		P prv, now = q.front(), nxt;
		q.pop();
		if (s[now.x][now.y] == 'T') {
			unsigned int cntnow = 0;
			for (unsigned int i = 0; i < now.ans.length(); i++)
				if (now.ans[i] >= 'A' && now.ans[i] <= 'Z') ++cntnow;
			if (cntnow < cntans || (cntnow == cntans && now.ans.length() < cnt)) {
				ans = now.ans;
				cntans = cntnow;
				cnt = now.ans.length();
			}
			continue;
		}
		//0-3上下左右
		for (int i = 0; i < 4; i++) {
			nxt = now;
			nxt.x += d[i][0];
			nxt.y += d[i][1];
			if (!pd(nxt.x, nxt.y)) continue;
			if (v[nxt.x][nxt.y][i]) continue;
			prv = now;
			prv.x-=d[i][0];
            prv.y-=d[i][1];
			if (!bfs2(prv, now)) continue;
			v[nxt.x][nxt.y][i] = 1;
			nxt.ans += tmp;
			nxt.ans += A[i];
			nxt.px = now.x;
			nxt.py = now.y;
			q.push(nxt);
		}
	}
	return ans;
}

void Pushing_Boxes() {
	for (int i = 1; i <= r; i++) cin >> (s[i] + 1);
	cout << "Maze #" << ++num << endl << bfs1() << endl << endl;
}

int main() {
	while (cin >> r >> c && r && c) Pushing_Boxes();
	return 0;
}

C++(clang++11) 解法, 执行用时: 21ms, 内存消耗: 376K, 提交时间: 2021-03-03 19:04:41

#include<bits/stdc++.h> 
using namespace std;
const int N=30;
int n,m,num,dx[4]={-1,1,0,0},dy[4]={0,0,-1,1},tot;
bool vis[N][N][4],vs[N][N];
char s[N][N],B[4]={'N','S','W','E'},P[4]={'n','s','w','e'};	//上下左右 
string path,ans,str;
struct node{
	int x,y,dir;
	string ans;	
};
queue<node>q,tmp;
bool not_ok(int x,int y){
	return x<1||x>n||y<1||y>m||s[x][y]=='#';
}
bool bfs_man(int s,int t,int u,int v,int x,int y){	//(s,t)->(u,v) 绕过 (x,y) 
	if(s==u&&t==v) return path="",1;
	queue<node>q;
	path="",memset(vs,0,sizeof(vs));
	vs[s][t]=vs[x][y]=1,q.push((node){s,t,0,""}); 
	while(q.size()){
		int x=q.front().x,y=q.front().y;
		str=q.front().ans,q.pop();
		for(int k=0;k<4;k++){
			int i=x+dx[k],j=y+dy[k];
			if(not_ok(i,j)||vs[i][j]) continue;
			vs[i][j]=1,path=str+P[k],q.push((node){i,j,k,path});
			if(i==u&&j==v) return 1;
		}
	}
	return 0;
}
void expand(node a,bool flag=0){	//扩展 
	int x=a.x,y=a.y,dir=a.dir;
	for(int k=0;k<4;k++){
		if(flag&&dir!=k) continue;	//flag=1: 扩展方向给定 
		int i=x+dx[k],j=y+dy[k],u=x-dx[k],v=y-dy[k],S=x-dx[dir],T=y-dy[dir];	//new_box new_man now_man 
		if(not_ok(i,j)||not_ok(u,v)||not_ok(S,T)||vis[i][j][k]||!bfs_man(S,T,u,v,x,y)) continue;
		str=a.ans+path+B[k],vis[i][j][k]=1,tmp.push((node){i,j,k,str});
		if(s[i][j]=='T'){
			int cnt=0;
			for(int i=0;i<(int)str.size();i++)
				if(str[i]>='A'&&str[i]<='Z') cnt++;
			if(ans==""||cnt<tot||(cnt==tot&&str.size()<ans.size())) ans=str,tot=cnt;
		} 
	}
}
string bfs_box(){
	swap(q,tmp);
	while(q.size()){
		while(q.size()) expand(q.front()),q.pop();
		swap(q,tmp);
	}
	return ans==""?"Impossible.":ans;
}
signed main(){
	while(~scanf("%d%d",&n,&m)&&n&&m){
		int x,y,S,T;
		for(int i=1;i<=n;i++){ 
			scanf("%s",s[i]+1);
			for(int j=1;j<=m;j++){
				if(s[i][j]=='B') x=i,y=j;
				else if(s[i][j]=='S') S=i,T=j;
			}
		} 
		memset(vis,0,sizeof(vis)),path=ans="",tot=0,queue<node>().swap(tmp);
		for(int k=0;k<4;k++){
			int i=x-dx[k],j=y-dy[k];
			if(not_ok(i,j)||!bfs_man(S,T,i,j,x,y)) continue;
			expand((node){x,y,k,path},1);
		}
		printf("Maze #%d\n%s\n\n",++num,bfs_box().c_str());
	}
	return 0;
}

上一题