POJ 3322 Bloxorz I

来源:互联网 发布:java解压包的后缀 编辑:程序博客网 时间:2024/05/01 18:37

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<cstdio>#include<iostream>#include<cstring>#include<map>#include<queue>#include<stack>#include<algorithm>#include<vector>#include<cmath>#include<string>#include<functional>using namespace std;const int maxn=505;int T,n,m,f[maxn][maxn][3];char s[maxn][maxn];struct point{int x,y,state,cnt;point(int x=0,int y=0,int state=0,int cnt=0):x(x),y(y),state(state),cnt(cnt){};bool operator ==(const point&a){return (a.x==x&&a.y==y&&a.state==state);}point move(int mv){point now;if (state==0){if (mv==0) now=point(x-2,y,2,cnt+1);if (mv==1) now=point(x,y+1,1,cnt+1);if (mv==2) now=point(x+1,y,2,cnt+1);if (mv==3) now=point(x,y-2,1,cnt+1);}if (state==1){if (mv==0) now=point(x-1,y,1,cnt+1);if (mv==1) now=point(x,y+2,0,cnt+1);if (mv==2) now=point(x+1,y,1,cnt+1);if (mv==3) now=point(x,y-1,0,cnt+1);}if (state==2){if (mv==0) now=point(x-1,y,0,cnt+1);if (mv==1) now=point(x,y+1,2,cnt+1);if (mv==2) now=point(x+2,y,0,cnt+1);if (mv==3) now=point(x,y-1,2,cnt+1);}return now;}}bg,ed;void begin(){for (int i=1;i<=n;i++) for (int j=1;j<=m;j++)if (s[i][j]=='O') {ed=point(i,j,0); return;}}void End(){for (int i=1;i<=n;i++) for (int j=1;j<=m;j++)if (s[i][j]=='X') {if (j+1<=m&&s[i][j+1]=='X') {bg=point(i,j,1); return;}if (i+1<=n&&s[i+1][j]=='X') {bg=point(i,j,2); return;}bg=point(i,j,0);return; }}bool check(point &a){if (a.x<1||a.x>n) return false;if (a.y<1||a.y>m) return false;if (s[a.x][a.y]=='#') return false;if (f[a.x][a.y][a.state]>=0) return false;if (a.state==0){if (s[a.x][a.y]=='E') return false;}elseif (a.state==1){if (a.y+1>m) return false;if (s[a.x][a.y+1]=='#') return false;}else {if (a.x+1>n) return false;if (s[a.x+1][a.y]=='#') return false;}return true;}int bfs(){memset(f,-1,sizeof(f));queue<point> p;p.push(bg);f[bg.x][bg.y][bg.state]=0;while (!p.empty()){point q=p.front();p.pop();if (q==ed) break;for (int i=0;i<4;i++){point now=q.move(i);if (check(now)){p.push(now);f[now.x][now.y][now.state]=q.cnt+1;}}}return f[ed.x][ed.y][ed.state];}int main(){while (cin>>n>>m,n+m){for (int i=1;i<=n;i++) scanf("%s",s[i]+1);begin();End();int ans=bfs();if (ans>=0) printf("%d\n",ans);else printf("Impossible\n");}return 0;}


0 0
原创粉丝点击