这题花了我一个一个一个下午,甚至114514分钟
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
翻译
可恶的poj都是英文,虽然看得懂。
有一个
1
∗
1
∗
2
1*1*2
1∗1∗2的磁铁,在一个
n
∗
m
n*m
n∗m的板子上动。#不能走,E只能躺着过。O是终点,立在终点上就算成功。
思路
三种状态:立着、横着躺、竖着躺。
剩下广搜就行了。
代码
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int N = 510;
struct node
{
int x, y;
int lie;
};
char s[N][N];
node st, ed;
int n, m;
int dis[N][N][3];
queue<node> q;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
bool valid1(int x, int y)
{
return x >= 0 && y >= 0 && x < n && y < m;
}
bool valid(node t)
{
if(!valid1(t.x, t.y)) return false;
if(s[t.x][t.y] == '#') return false;
if(t.lie == 0 && s[t.x][t.y] != '.') return false;
if(t.lie == 1 && s[t.x][t.y + 1] == '#') return false;
if(t.lie == 2 && s[t.x + 1][t.y] == '#') return false;
return true;
}
int next_x[3][4] = {{0, 0, -2, 1}, {0, 0, -1, 1}, {0, 0, -1, 2}};
int next_y[3][4] = {{-2, 1, 0, 0}, {-1, 2, 0, 0}, {-1, 1, 0, 0}};
int next_lie[3][4] = {{1, 1, 2, 2}, {0, 0, 1, 1}, {2, 2, 0, 0}};
int bfs()
{
memset(dis, -1, sizeof(dis));
while(q.size()) q.pop();
dis[st.x][st.y][st.lie] = 0;
q.push(st);
while(q.size())
{
node tmp = q.front();
q.pop();
for(int i = 0; i < 4; i++)
{
node _new;
_new.x = tmp.x + next_x[tmp.lie][i];
_new.y = tmp.y + next_y[tmp.lie][i];
_new.lie = next_lie[tmp.lie][i];
if(!valid(_new)) continue;
if(dis[_new.x][_new.y][_new.lie] == -1)
{
dis[_new.x][_new.y][_new.lie] = dis[tmp.x][tmp.y][tmp.lie] + 1;
q.push(_new);
if(_new.x == ed.x && _new.y == ed.y && _new.lie == ed.lie) return dis[_new.x][_new.y][_new.lie];
}
}
}
return -1;
}
int main()
{
while(~scanf("%d%d", &n, &m) && n && m)
{
for(int i = 0; i < n; i++) scanf("%s", s[i]);
for (int i = 0; i < n ; i++)
{
for(int j = 0; j < m; j++)
{
if(s[i][j] == 'O')
{
ed.x = i;
ed.y = j;
ed.lie = 0;
s[i][j] = '.';
}
else if(s[i][j] == 'X')
{
for(int k = 0; k < 4; k++)
{
int x = i + dx[k];
int y = j + dy[k];
if(valid1(x, y) && s[x][y] == 'X')
{
st.x = min(i, x);
st.y = min(j, y);
st.lie = k < 2 ? 1 :2;
s[i][j] = s[x][y] = '.';
break;
}
}
if(s[i][j] == 'X')
{
st.x = i;
st.y = j;
st.lie = 0;
}
}
}
}
int ans = bfs();
if(ans == -1) cout << "Impossible" << endl;
else cout << ans << endl;
}
return 0;
}