这题知道是个迷宫问题,我既然广度半天广度不明白,,,看了大佬的代码才知道原来自己有多菜。。。。。(虽然一直很菜)
AC代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 20;
int d[4][2] = { {1,0},{0,-1},{0,1},{-1,0} }; //D,L,R,U
int mirror(int i) {
if (i == 1 || i == 2) {
return i ^ 3;
}
else
return i;
}
pair<int, int> move(string g[N], int x, int y, int i) {
int x1 = x + d[i][0];
int y1 = y + d[i][1];
if (x1 < 0 || x1 >= N || y1 < 0 || y1 >= N || g[x1][y1] == '#') {
return { x,y };
}
return { x1,y1 };
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
string a[N], b[N];
for (int i = 0; i < N; i++) cin >> a[i] >> b[i];
int f[N][N][N][N];
int g[N][N][N][N];
memset(f, -1, sizeof(f));
queue<tuple<int, int, int, int>> q;
q.emplace(N - 1, N - 1, N - 1, 0);
f[N - 1][N - 1][N - 1][0] = 0;
while (!q.empty()) {
auto [x1, y1, x2, y2] = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
auto [nx1, ny1] = move(a, x1, y1, i);
auto [nx2, ny2] = move(b, x2, y2, mirror(i));
if (f[nx1][ny1][nx2][ny2] < 0) {
f[nx1][ny1][nx2][ny2] = f[x1][y1][x2][y2] + 1;
g[nx1][ny1][nx2][ny2] = (((x1 * N + y1) * N + x2) * N + y2) * 4 + i;//这个数据压缩实属厉害,第一次见。。菜了
q.emplace(nx1, ny1, nx2, ny2);
}
}
}
cout << f[0][N - 1][0][0] << endl;
int x1 = 0,y1 = N-1,x2 = 0,y2 = 0;
string ans;
while(true){
a[x1][y1] = b[x2][y2] = 'A';
if(f[x1][y1][x2][y2] == 0){
break;
}
int G = g[x1][y1][x2][y2];
x1 = G / 4 / N / N / N;
y1 = G / 4 / N / N % N;
x2 = G / 4 / N % N;
y2 = G / 4 % N;
int d = G % 4;
ans += "DLRU"[d];
}
reverse(ans.begin(), ans.end());
cout << ans << endl;
for(int i = 0;i<N;i++)
cout << a[i] << " " << b[i] << endl;
return 0;
}