题目大意
-
游戏设定
有两个并排的 20×20 的迷宫棋盘,中间用空格隔开
每个格子要么是空地(.),要么是砖块(#)
砖块阻挡前进,企鹅不能进入砖块格子
-
企鹅位置与目标
左企鹅:从 (20,20) 出发 → 目标 (1,20)
坐标说明:第一个数是行号,第二个数是列号
在代码中我们转换为0-index:(19,19) → (0,19)
右企鹅:从 (20,1) 出发 → 目标 (1,1)
在代码中:(19,0) → (0,0)
-
移动规则(关键)
玩家可以发出四个指令之一,但两只企鹅的移动是镜像对称的:
指令 左企鹅移动 右企鹅移动 说明
L 向左走 向右走 镜像对称
R 向右走 向左走 镜像对称
U 向上走 向上走 相同方向
D 向下走 向下走 相同方向
重要特性:如果企鹅前方有砖块阻挡,它会原地不动,另一只企鹅可能正常移动。
-
游戏结束条件
两只企鹅必须同时到达各自的目标位置。不过,一只企鹅可以先到终点,然后离开,再回来。
-
输出要求
输入第一行 x:
如果 x=1:只输出最少步数
如果 x=2:输出步数 + 操作序列 + 带路径的地图
字典序要求:D < L < R < U
如果有多个最短路径,选择字典序最小的
思路
状态转移
每个状态可以尝试 4 种操作(D, L, R, U),生成新状态:
1.分别计算两只企鹅的新位置
2.检查是否碰到砖块/边界
3.如果非法,则企鹅原地不动
4.两只企鹅独立判断,可能一只动一只不动
目标状态
目标状态 = (0, 19, 0, 0) // 0-index坐标
BFS(广度优先搜索)—— 正解
为什么BFS适合?
要求最少步数 → BFS天然找到最短路径
状态空间有限(160,000) → BFS可以处理
字典序最小 → BFS扩展时按顺序尝试即可
详细步骤
1.数据输入与处理
// 1. 读取x值
// 2. 读取20行,每行41字符
// 3. 解析为左右两个20×20的布尔数组
// true表示可通行('.'),false表示砖块('#')2.状态表示与编码
// 使用0-index坐标
struct Node{
int r1,c1; // 左企鹅
int r2,c2; // 右企鹅
};
// 编码函数:将4维坐标压缩为1维整数
int encode(int r1,int c1,int r2,int c2){
return ((r1*20+c1)*20+r2)*20+c2;
}3.BFS搜索
// 初始化
queue<int> q; // 存储编码后的状态
int dist[MAXSTATES]; // 到达每个状态的步数
int pstart[MAXSTATES];// 前一个状态
char pmove[MAXSTATES];// 到达当前状态的操作
// 起点入队
dist[起点编码] = 0;
q.push(起点编码);
// BFS主循环
while (!q.empty()) {
当前状态 = q.front(); q.pop();
if (当前状态 == 目标状态) break;
// 尝试4种移动:按D,L,R,U顺序
for (每种移动) {
计算两只企鹅的新位置;
检查是否合法(不撞墙);
编码新状态;
if (新状态未访问过) {
dist[新状态] = dist[当前状态] + 1;
prev_state[新状态] = 当前状态;
prev_move[新状态] = 移动指令;
q.push(新状态);
}
}
}4.路径重建
// 从终点回溯到起点
vector<int> pathstates;
string moves;
当前状态 = 目标状态编码;
while (当前状态 != 起点状态编码) {
pathstates.push_back(当前状态);
moves += pmove[当前状态];
当前状态 = pstart[当前状态];
}
// 加入起点
pathstates.push_back(起点状态编码);
// 反转得到正确顺序
reverse(moves.begin(), moves.end());
reverse(pathstates.begin(), pathstates.end());5.输出
// 标记所有访问过的格子
bool leftvis[20][20] = {false};
bool rightvis[20][20] = {false};
for (每个状态 in pathstates) {
解码状态;
leftvis[左企鹅行][左企鹅列] = true;
rightvis[右企鹅行][右企鹅列] = true;
}
// 输出地图
for (行 i = 0 to 19) {
// 左地图
for (列 j = 0 to 19) {
if (leftvis[i][j]) cout << 'A';
else if (可通行) cout << '.';
else cout << '#';
}
cout << ' ';
// 右地图
for (列 j = 0 to 19) {
if (rightvis[i][j]) cout << 'A';
else if (可通行) cout << '.';
else cout << '#';
}
cout << endl;
}代码
#include<bits/stdc++.h>
using namespace std;
const int N=20;
const int MAXSTATES=20*20*20*20;
const char moves[4]={'D','L','R','U'};
const int dr1[4]={1,0,0,-1};//D:下 L:不动 R:不动 U:上
const int dc1[4]={0,-1,1,0};
const int dr2[4]={1,0,0,-1};
const int dc2[4]={0,1,-1,0};
struct Node{
int r1,c1;
int r2,c2;
Node(){};
Node(int r1,int c1,int r2,int c2):
r1(r1),c1(c1),r2(r2),c2(c2){}
};
int encode(int r1,int c1,int r2,int c2){
return ((r1*N+c1)*N+r2)*N+c2;
}//将状态 编码为一个整数,方便存储
Node decode(int code){
Node s;
s.c2=code%N;
code/=N;
s.r2=code%N;
code/=N;
s.c1=code%N;
code/=N;
s.r1=code;
return s;
}
int main(){
int x;
cin >> x;
cin.ignore(100,'\n');
string lines[20];
bool left[20][20];
bool right[20][20];
for(int i=0;i<20;i++){
getline(cin,lines[i]);
for(int j=0;j<20;j++){
left[i][j]=(lines[i][j]=='.');
}
for(int j=0;j<20;j++){
right[i][j]=(lines[i][j+21]=='.');
}
}
Node start(19,19,19,0);
Node target(0,19,0,0);
int dist[MAXSTATES];//到达每个状态所需要的步数
int pstart[MAXSTATES];//前一个状态
char pmove[MAXSTATES];//从前一个状态到当前状态的动作
memset(pstart,-1,sizeof(pstart));
memset(pmove,0,sizeof(pmove));
memset(dist,-1,sizeof(dist));//初始化
queue<int> q;
int startcode=encode(start.r1,start.c1,start.r2,start.c2);
int targetcode=encode(target.r1,target.c1,target.r2,target.c2);
dist[startcode]=0;
pstart[startcode]=-1;
q.push(startcode);
while(!q.empty()){
int curcode=q.front();
q.pop();
if(curcode==targetcode){
break;
}
Node cur=decode(curcode);
for(int i=0;i<4;i++){
//左企鹅
int nr1=cur.r1+dr1[i];
int nc1=cur.c1+dc1[i];
if(nr1<0||nr1>=N||nc1<0||nc1>=N||!left[nr1][nc1]){
nr1=cur.r1;
nc1=cur.c1;
}
//右企鹅
int nr2=cur.r2+dr2[i];
int nc2=cur.c2+dc2[i];
if(nr2<0||nr2>=N||nc2<0||nc2>=N||!right[nr2][nc2]){
nr2=cur.r2;
nc2=cur.c2;
}
int nexcode=encode(nr1,nc1,nr2,nc2);
if(dist[nexcode]==-1){
dist[nexcode]=dist[curcode]+1;
pstart[nexcode]=curcode;
pmove[nexcode]=moves[i];
q.push(nexcode);
}
}
}
vector<int> statecodes;//存储路径上的所有状态
string path;//存储操作序列
int cur=targetcode;
while(cur!=startcode){
statecodes.push_back(cur);
path+=pmove[cur];
cur=pstart[cur];
}
statecodes.push_back(startcode);
//反转路径,因为我们是从终点回溯到起点的
reverse(path.begin(),path.end());
reverse(statecodes.begin(),statecodes.end());
cout << path.length() << endl;
if(x==2){
cout << path << endl;
bool leftvis[20][20]={false};
bool rightvis[20][20]={false};
for(int i=0;i<statecodes.size();i++){
Node s=decode(statecodes[i]);
leftvis[s.r1][s.c1]=true;
rightvis[s.r2][s.c2]=true;
}
for(int i=0;i<20;i++){
for(int j=0;j<20;j++){
if(leftvis[i][j]){
cout << 'A';
}else if(left[i][j]){
cout << '.';
}else{
cout << '#';
}
}
cout << ' ';
for(int j=0;j<20;j++){
if(rightvis[i][j]){
cout << 'A';
}else if(right[i][j]){
cout << '.';
}else{
cout << '#';
}
}
cout << endl;
}
}
return 0;
}