POJ3322 Bloxorz I 题解

前言

20min内单独现写AC此题,DFS、BFS基本过关了。

题目

链接

http://poj.org/problem?id=3322

字面描述

Bloxorz I
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 9411 Accepted: 2936
Description

Little Tom loves playing games. One day he downloads a little computer game called ‘Bloxorz’ which makes him excited. It’s a game about rolling a box to a specific position on a special plane. Precisely, the plane, which is composed of several unit cells, is a rectangle shaped area. And the box, consisting of two perfectly aligned unit cube, may either lies down and occupies two neighbouring cells or stands up and occupies one single cell. One may move the box by picking one of the four edges of the box on the ground and rolling the box 90 degrees around that edge, which is counted as one move. There are three kinds of cells, rigid cells, easily broken cells and empty cells. A rigid cell can support full weight of the box, so it can be either one of the two cells that the box lies on or the cell that the box fully stands on. A easily broken cells can only support half the weight of the box, so it cannot be the only cell that the box stands on. An empty cell cannot support anything, so there cannot be any part of the box on that cell. The target of the game is to roll the box standing onto the only target cell on the plane with minimum moves.

The box stands on a single cell

The box lies on two neighbouring cells, horizontally

The box lies on two neighbouring cells, vertically
After Little Tom passes several stages of the game, he finds it much harder than he expected. So he turns to your help.

Input

Input contains multiple test cases. Each test case is one single stage of the game. It starts with two integers R and C(3 ≤ R, C ≤ 500) which stands for number of rows and columns of the plane. That follows the plane, which contains R lines and C characters for each line, with ‘O’ (Oh) for target cell, ‘X’ for initial position of the box, ‘.’ for a rigid cell, ‘#’ for a empty cell and ‘E’ for a easily broken cell. A test cases starts with two zeros ends the input.

It guarantees that

There’s only one ‘O’ in a plane.
There’s either one ‘X’ or neighbouring two 'X’s in a plane.
The first(and last) row(and column) must be ‘#’(empty cell).
Cells covered by ‘O’ and ‘X’ are all rigid cells.
Output

For each test cases output one line with the minimum number of moves or “Impossible” (without quote) when there’s no way to achieve the target cell.

Sample Input

7 7
#######
#…X###
#…##O#
#…E#
#…E#
#…#
#######
0 0
Sample Output

10
Source

POJ Monthly–2007.08.05, Rainer

思路

此题最大的问题就是存图,又2个点的情况,又有1个点的情况如何存储状态呢?

2个点的坐标一起存,耗费的空间为O((nm)^2),MLE了;

但2个点的坐标一定相邻,所以存一个核心点的坐标,还有一个所属状态,状态分三种:单个点,竖躺,横躺。竖躺核心点统一记录上面的点,横躺同一记录左面的点。开始写吧

代码实现

#include<cstdio>
#include<queue>
using namespace std;

// 0 单点  1 竖躺 2 横躺 
const int maxn=500+10;
const int inf=1e9;
//  上 下 左 右 
//状态为i,像j号方向转动,核心点坐标及状态的变化
const int dx[3][4]=  {{-2,1, 0,0},{-1,2, 0,0},{-1,1, 0,0}};
const int dy[3][4]=  {{ 0,0,-2,1},{ 0,0,-1,1},{ 0,0,-1,2}};
const int dlie[3][4]={{ 1,1, 2,2},{ 0,0, 1,1},{ 2,2, 0,0}};
int n,m;
char a[maxn][maxn];
int dis[maxn][maxn][5];
struct vertex{
	int x,y,lie;
};
vertex st,ed;
queue<vertex>q;
//初始化
inline void init(){
	st.x=st.y=st.lie=ed.x=ed.y=ed.lie=0;
	while(!q.empty())q.pop();
	for(int i=1;i<=n+1;i++){
		for(int j=1;j<=m+1;j++){
			for(int k=0;k<3;k++)dis[i][j][k]=inf;
		}
	}
}
//是否在界内
inline bool inbound(int nx,int ny){return nx>=1&&nx<=n&&ny>=1&&ny<=m;}
//是否合法
inline bool in_law(vertex op){
	if(!inbound(op.x,op.y))return false;
	if(a[op.x][op.y]=='#')return false;
	if(op.lie==1&&a[op.x+1][op.y]=='#')return false;
	if(op.lie==2&&a[op.x][op.y+1]=='#')return false;
	if(op.lie==0&&a[op.x][op.y]=='E')return false;
	return true;
}
//bfs
inline int bfs(){
	dis[st.x][st.y][st.lie]=0;
	q.push(st);
	while(!q.empty()){
		vertex point=q.front();
		//printf("%d %d %d\n",point.x,point.y,point.lie);
		q.pop();
		for(int i=0;i<4;i++){
			vertex nxt;
			nxt.x=point.x+dx[point.lie][i];
			nxt.y=point.y+dy[point.lie][i];
			nxt.lie=dlie[point.lie][i];
			if(!in_law(nxt))continue;
			if(dis[nxt.x][nxt.y][nxt.lie]==inf){
				dis[nxt.x][nxt.y][nxt.lie]=dis[point.x][point.y][point.lie]+1;
				q.push(nxt);
				//printf("%d %d %d\n",nxt.x,nxt.y,nxt.lie);
				if(nxt.x==ed.x&&nxt.y==ed.y&&nxt.lie==ed.lie)return dis[ed.x][ed.y][ed.lie];
			}
		}
	}
	return inf;
}
int main(){
	while(1){
		scanf("%d%d",&n,&m);
		if(n==0&&m==0)break;
		init();
		//求起始及终点坐标及状态
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				scanf(" %c",&a[i][j]);
				if(a[i][j]=='X'){
					if(st.x!=0&&st.y!=0){
						if(j-1==st.y)st.lie=2; 
						else if(i-1==st.x)st.lie=1;
					}
					else st.x=i,st.y=j;
				}
				else if(a[i][j]=='O')ed.x=i,ed.y=j;
			}
		}
		//printf("%d %d %d\n",ed.x,ed.y,ed.lie);
		int ans=bfs();
		if(ans==inf)printf("Impossible\n");
		else printf("%d\n",ans);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

materialistOier

我只是一名ssfoier

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值