跳到正文

题解

题解:P10485 Bloxorz I

Zephyn约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年3月7日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 3 分钟
获赞
1
题目
P10485

版本

  1. v2当前
  2. v1

题意

就是个模拟,题意很清晰了

思路

不难发现,这就是个升级版迷宫,加入了方块的多种状态,我们只需升级一下方向数组就行了(L10~L18)。

这边看到很多人都把判断终点状态的代码加上了,其实不用,题目已经说了终点只可能是一个格子,所以不用判断。

~~最重要的一点(对我来说),~~开 O2 了记得初始化变量,不管是局部还是全局。

代码

C++
#include <bits/stdc++.h>
using namespace std;
struct blk{
	int x,y,pt,stp;
};
const int N = 5e2 + 5,INF = 0x7fffffff;
int n,m;
char a[N][N];
int spt,sx,sy,mn;
int dx[5][5] = {{-2,1,0,0},
				{-1,1,0,0},
				{-1,2,0,0}},
	dy[5][5] = {{0,0,-2,1},
				{0,0,-1,2},
				{0,0,-1,1}},
	dpt[5][5] = {{2,2,1,1},
				 {1,1,0,0},
				 {0,0,2,2}};
int f[N][N][5];
void bfs(int sx,int sy,int spt){
	queue<blk> q;
	q.push({sx,sy,spt,0});
	while (!q.empty()){
		int x = q.front().x,y = q.front().y,pt = q.front().pt,stp = q.front().stp;
		q.pop();
		if (a[x][y] == 'O' && !pt){
			mn = stp;
			return;
		}
		if (a[x][y] == '#' || !pt && a[x][y] == 'E' ||
			pt == 1 && a[x][y + 1] == '#' ||
			pt == 2 && a[x + 1][y] == '#' ||
			f[x][y][pt] <= stp)	continue;	// 记得加等于号
		// printf("%d %d %d %d\n",x,y,pt,stp);
		f[x][y][pt] = stp;
		for (int i = 0;i < 4;i++){
			int xi = x + dx[pt][i],yi = y + dy[pt][i],pti = dpt[pt][i];
			q.push({xi,yi,pti,stp + 1});
		}
	}
}
int main(int argc, char **argv){
	cin >> n >> m;
	while (n || m){
		mn = INF;
		memset(f,0x7f,sizeof f);
		for (int i = 1;i <= n;i++){
			for (int j = 1;j <= m;j++){
				cin >> a[i][j];
				if (a[i][j] == 'X'){
					if (a[i][j - 1] == 'X')	sx = i,sy = j - 1,spt = 1;
					else if (a[i - 1][j] == 'X')	sx = i - 1,sy = j,spt = 2;
					else	sx = i,sy = j,spt = 0;	// 开O2的把`spt=0`加上
				}
			}
		}
		bfs(sx,sy,spt);
		if (mn == INF)	cout << "Impossible\n";
		else	printf("%d\n",mn);
		cin >> n >> m;
	}
	return 0;
}
原文