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.
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<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
struct Node {
int x,y;
int state,step;
}node[555];
int n,m,ex,ey;
char mp[555][555];
bool vis[555][555][4];
queue<Node>q;
bool island(int x,int y) {
if(x>=1&&x<=n&&y>=1&&y<=m)
return true;
return false;
}
void bfs() {
Node now,next;
while(!q.empty()) {
now=q.front();
q.pop();
if(!island(now.x,now.y)) continue;
if(now.state==1&&now.x+1>n) continue;
if(now.state==2&&now.y+1>m) continue;
if(now.state==0&&mp[now.x][now.y]=='E') continue;
if(now.x==ex&&now.y==ey&&now.state==0) {
printf("%d\n",now.step);
return;
}
if(now.state==0) {
if(!vis[now.x-2][now.y][1]&&now.x-2>=1) {
next.x=now.x-2;
next.y=now.y;
next.state=1;
next.step=now.step+1;
q.push(next);
vis[next.x][next.y][1]=1;
}
if(!vis[now.x+1][now.y][1]&&now.x+1<=n) {
next.x=now.x+1;
next.y=now.y;
next.state=1;
next.step=now.step+1;
q.push(next);
vis[next.x][next.y][1]=1;
}
if(!vis[now.x][now.y+1][2]&&now.y+1<=m) {
next.x=now.x;
next.y=now.y+1;
next.state=2;
next.step=now.step+1;
q.push(next);
vis[next.x][next.y][2]=1;
}
if(!vis[now.x][now.y-2][2]&&now.y-2>=1) {
next.x=now.x;
next.y=now.y-2;
next.state=2;
next.step=now.step+1;
q.push(next);
vis[next.x][next.y][2]=1;
}
}
if(now.state==1) {
if(!vis[now.x-1][now.y][0]&&mp[now.x-1][now.y]!='E'&&now.x-1>=1) {
next.x=now.x-1;
next.y=now.y;
next.state=0;
next.step=now.step+1;
q.push(next);
vis[next.x][next.y][0]=1;
}
if(!vis[now.x+2][now.y][0]&&mp[now.x+2][now.y]!='E'&&now.x+1<=n) {
next.x=now.x+2;
next.y=now.y;
next.state=0;
next.step=now.step+1;
q.push(next);
vis[next.x][next.y][0]=1;
}
if(!vis[now.x][now.y+1][1]&&now.y+1<=m) {
next.x=now.x;
next.y=now.y+1;
next.state=1;
next.step=now.step+1;
q.push(next);
vis[next.x][next.y][1]=1;
}
if(!vis[now.x][now.y-1][1]&&now.y-1>=1) {
next.x=now.x;
next.y=now.y-1;
next.state=1;
next.step=now.step+1;
q.push(next);
vis[next.x][next.y][1]=1;
}
}
if(now.state==2) {
if(!vis[now.x][now.y-1][0]&&mp[now.x][now.y-1]!='E'&&now.y-1>=1) {
next.x=now.x;
next.y=now.y-1;
next.state=0;
next.step=now.step+1;
q.push(next);
vis[next.x][next.y][0]=1;
}
if(!vis[now.x][now.y+2][0]&&mp[now.x][now.y+2]!='E'&&now.y+2<=m) {
next.x=now.x;
next.y=now.y+2;
next.state=0;
next.step=now.step+1;
q.push(next);
vis[next.x][next.y][0]=1;
}
if(!vis[now.x-1][now.y][2]&&now.x-1>=1) {
next.x=now.x-1;
next.y=now.y;
next.state=2;
next.step=now.step+1;
q.push(next);
vis[next.x][next.y][2]=1;
}
if(!vis[now.x+1][now.y][2]&&now.x+1<=n) {
next.x=now.x+1;
next.y=now.y;
next.state=2;
next.step=now.step+1;
q.push(next);
vis[next.x][next.y][2]=1;
}
}
}
printf("Impossible\n");//!!!
}
int main() {
while(~scanf("%d%d",&n,&m)) {
int i,j,cnt=0;
Node start;
if(n+m==0) break;
while(!q.empty()) q.pop();
memset(vis,0,sizeof(vis));
for(i=1;i<=n;++i) {
scanf("%s",mp[i]+1);
for(j=1;j<=m;++j) {
if(mp[i][j]=='O') {
ex=i;
ey=j;
}
if(mp[i][j]=='#') {
vis[i][j][0]=1;
vis[i][j][1]=1;
vis[i][j][2]=1;
if(i>=2) vis[i-1][j][1]=1;
if(j>=2) vis[i][j-1][2]=1;
}
if(mp[i][j]=='X') cnt++;
}
}
for(i=1;i<=n;++i) {
for(j=1;j<=m;++j) {
if(mp[i][j]=='X') {
if(cnt==1) {
start.x=i;
start.y=j;
start.state=0;
start.step=0;
q.push(start);
vis[i][j][0]=1;
}
if(cnt==2&&mp[i+1][j]=='X') {
start.x=i;
start.y=j;
start.state=1;
start.step=0;
q.push(start);
vis[i][j][1]=1;
}
if(cnt==2&&mp[i][j+1]=='X') {
start.x=i;
start.y=j;
start.state=2;
start.step=0;
q.push(start);
vis[i][j][2]=1;
}
break;
}
}
}
bfs();
}
return 0;
}