POJ-3322 bfs

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

没看书自己写程序的有点啰嗦

#include<iostream>
#include<queue>
#include<vector>
#include<iomanip>
#include<string.h>
#define p(i,j) make_pair(i,j)
using namespace std;
typedef long long ll;
const int N=503;
int n,m,sx,sy,tx,ty;
char mp[N][N];
struct st{
	int x,y,step,op;//op=-1:立着;else: 躺着且其中一格在(x,y),其朝向为op方向
	st(){}
	st(int x,int y,int step,int op=-1):x(x),y(y),step(step),op(op){}
}now,nex;
int F[4][2]={1,0,0,1,-1,0,0,-1};
int v0[N][N],v1[N][N][4];//立着/躺着是否经过地图
int nx,ny;
queue<st>q;
bool out(int x,int y){return x<0||y<0||x>=n||y>=m;}//出界
bool inv(int x,int y,int u,int v){return mp[x][y]=='#'||mp[u][v]=='#';}//禁止
inline void gao(int u,int i){//保持躺着的前提下,向两边滚动
	nex=st(now.x+F[u][0],now.y+F[u][1],now.step+1,now.op);
	nx=nex.x+F[i][0],ny=nex.y+F[i][1];
	if(!out(nex.x,nex.y)&&!out(nx,ny)&&!inv(nex.x,nex.y,nx,ny)&&!v1[nex.x][nex.y][i])
		v1[nex.x][nex.y][i]=1,q.push(nex);
}
int bfs(){
	while(!q.empty())q.pop();
	q.push(now);
	while(!q.empty()){
		now=q.front(),q.pop();
		if(now.x==tx&&now.y==ty&&now.op==-1)return cout<<now.step<<'\n',0;
		if(now.op==-1){//立着,向4方向躺下
			for(int i=0;i<4;++i){
				nex=st(now.x+F[i][0],now.y+F[i][1],now.step+1,i);
				nx=nex.x+F[i][0],ny=nex.y+F[i][1];
				if(out(nex.x,nex.y)||out(nx,ny)||inv(nx,ny,nex.x,nex.y)||v1[nex.x][nex.y][i])continue;
				v1[nex.x][nex.y][i]=1,q.push(nex);
			}continue;
		}//躺着,向2方向站起来
		int i=now.op;
		nx=now.x+(F[i][0]<<1),ny=now.y+(F[i][1]<<1);
		if(!out(nx,ny)&&!v0[nx][ny]&&mp[nx][ny]^'#'&&mp[nx][ny]^'E')v0[nx][ny]=1,q.push(st(nx,ny,now.step+1));
		nx=now.x-F[i][0],ny=now.y-F[i][1];
		if(!out(nx,ny)&&!v0[nx][ny]&&mp[nx][ny]^'#'&&mp[nx][ny]^'E')v0[nx][ny]=1,q.push(st(nx,ny,now.step+1));
		if(i==0||i==2){//继续躺着
			gao(1,i),gao(3,i);
			continue;
		}
		gao(0,i),gao(2,i);
	}
	return cout<<"Impossible\n",0;
}
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	while(cin>>n>>m,n||m){
		int cnt=0;
		memset(v0,0,sizeof(v0));
		memset(v1,0,sizeof(v1));
		for(int i=0;i<n;++i)
			for(int j=0;j<m;++j){
				cin>>mp[i][j];
				if(mp[i][j]=='X')now=++cnt==2?st(now.x,now.y,0,i==now.x?1:0):st(i,j,0);
				if(mp[i][j]=='O')tx=i,ty=j;
			}
		if(cnt==1)v0[now.x][now.y]=1;
		else v1[now.x][now.y][now.op]=1;
		bfs();
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值