链接
题目描述
在n*m的网格上滚箱子,每次可以让箱子在一个方向九十度立体翻倒或是立起来,但是不能碰到禁地,不能立在易碎地上,问从S到T要多少步(最后要立在T上)
样例输入
7 7
#######
#..X###
#..##O#
#....E#
#....E#
#.....#
#######
0 0 //输入结束标志
样例输出
9
思路
大模拟题,BFS
对每一种情况(横着,竖着,立着)分别讨论下一步的状态,然后瞎搞就可了
代码
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
int hx[5][5] = {{}, {0, -1, 1, 0, 0}, {0, -1, +2, 0, 0}, {0, -2, +1, 0, 0}};
int hy[5][5] = {{}, {0, 0, 0, -1, +2}, {0, 0, 0, -1, +1}, {0, 0, 0, -2, +1}};
int ht[5][5] = {{}, {0, 1, 1, 3, 3}, {0, 3, 3, 2, 2}, {0, 2, 2, 1, 1}};
char c[550][550];
int Ans[550][550][5];
int n, m, sx, sy, tx, ty, Lx;
struct lL
{
int x, y, t;
};
char read()
{
char cc;
cc = getchar();
while(cc != '#' && cc != '.' && cc != 'O' && cc != 'E' && cc != 'X') cc = getchar();
return cc;
}
bool check(int x, int y, int t)
{
if(x < 1 || y < 1 || Ans[x][y][t] > 0 || x > n || y > m) return 0;
if(t == 3)
if(c[x][y] == '#' || c[x][y] == 'E') return 0;//不能立在禁地或是易碎地上
if(t == 2)
if(c[x][y] == '#' || c[x + 1][y] == '#' || x + 1 > n) return 0;
if(t == 1)
if(c[x][y] == '#' || c[x][y + 1] == '#' || y + 1 > m) return 0;
return 1;
}
void bfs()
{
queue<lL>Q;
Q.push((lL){sx, sy, Lx});
Ans[sx][sy][Lx] = 1;
while(Q.size())
{
int x = Q.front().x, y = Q.front().y, t = Q.front().t;
Q.pop();
for(int i = 1; i <= 4; ++i)//找四个方向
{
int nx = x + hx[t][i], ny = y + hy[t][i], nt = ht[t][i];
if(check(nx, ny, nt) == 1)
{
Q.push((lL){nx, ny, nt});
Ans[nx][ny][nt] = Ans[x][y][t] + 1;
}
}
if(Ans[tx][ty][3]) break;
}
}
int main()
{
scanf("%d%d", &n, &m);
while(n != 0) {
Lx = 0;
memset(Ans, 0, sizeof(Ans));
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
{
c[i][j] = read();
if(c[i][j] == 'O') tx = i, ty = j;
}
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= m; ++j)
{
if(c[i][j] == 'X') {
sx = i; sy = j;
if(c[i][j + 1] == 'X')
Lx = 1;
else if(c[i + 1][j] == 'X')
Lx = 2;
else Lx = 3;
break;
}
}
if(Lx) break;
}
bfs();
if(Ans[tx][ty][3]) printf("%d\n", Ans[tx][ty][3] - 1);
else printf("Impossible\n");
scanf("%d%d", &n, &m);
}
return 0;
}