这题虽然不难想但是个人认为稍微有点实现难度,写的时候还是有点卡顿的((
首先题目说要求经过格子数量最少联想到最短路,那么要跑最短路我们肯定要建个图出来。
然后发现这个东西比较容易转化为图论建模,考虑光线可以从一个格子的上下左右射入和射出,但是放没放镜子会对入射和出射光线的方向做一些要求,所以我们把一个格子拆成上下左右四个点进行连边。
具体地,格子内按有没有镜子对应连边,格子和格子之间对应的点也要连边。注意到要求的是经过格子数量最少所以格子间的边权就是 ,格子内就是 。这里不太好写可以看代码。
处理一下起点和终点就能跑最短路了,然后记录一条最短路径和判无解就是平凡的,最后只剩下如何根据路径输出方案的问题了。
考虑按格子考虑,显然一个格子本来有镜子或者最短路没有经过这个格子里的任何一条边都是保留原来的。所以只考虑需要放新的镜子的情况,如果只包含一条边那么就是按那条边的镜子放,否则就是三条边那么就是取剩下那条边(因为格子内边权都是0可能会有三条边的情况),这里看起来有点难搞但是实际上不难可以自行看代码理解。
这样就搞完了,下班收工。
#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 1e6 + 5;
struct Edge {
int to, w, typ;
bool operator<(Edge nd) const {
return w > nd.w;
}
};
int T, n, m, sx, sy, gx, gy, dis[kMaxN], fr[kMaxN], typ[kMaxN];
vector<vector<char>> a;
vector<Edge> g[kMaxN];
int mp[256], val[256];
void clear() {
a.clear();
for (int i = 1; i <= n * m * 4; i++) g[i].clear();
}
int id(int i, int j) {
return (i - 1) * m + j;
}
pair<int, int> d_id(int x) {
return (pair<int, int>){(x + m - 1) / m, (x - 1) % m + 1};
}
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
int vis[kMaxN];
priority_queue<Edge> q;
void dijkstra(int s) {
fill(dis + 1, dis + n * m * 4 + 1, 1e9);
fill(vis + 1, vis + n * m * 4 + 1, 0);
for (dis[s] = 0, q.push({s, 0}); !q.empty();) {
int u = q.top().to;
q.pop();
if (vis[u]) continue;
vis[u] = 1;
for (auto ed : g[u]) {
int v = ed.to;
if (dis[u] + ed.w < dis[v]) {
dis[v] = dis[u] + ed.w;
fr[v] = u;
typ[v] = ed.typ;
q.push({v, dis[v]});
}
}
}
}
void solve() {
cin >> n >> m;
clear();
a.resize(n + 1);
for (int i = 1; i <= n; i++) {
a[i].resize(m + 1);
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
int x = (id(i, j) - 1) * 4;
for (int k = 0; k < 4; k++) {
int px = i + dx[k], py = j + dy[k];
if (px < 1 || py < 1 || px > n || py > m) continue;
int y = (id(px, py) - 1) * 4;
if (k % 2 == 0) {
g[x + k + 1].push_back({y + 2 + 1 - k, 1});
} else {
g[x + k + 1].push_back({y + 4 - k + 1, 1});
}
}
if (a[i][j] != '/') {
g[x + 0 + 1].push_back({x + 3 + 1, 0, 1});
g[x + 3 + 1].push_back({x + 0 + 1, 0, 1});
g[x + 1 + 1].push_back({x + 2 + 1, 0, 1});
g[x + 2 + 1].push_back({x + 1 + 1, 0, 1});
}
if (a[i][j] != '\\') {
g[x + 0 + 1].push_back({x + 1 + 1, 0, 0});
g[x + 1 + 1].push_back({x + 0 + 1, 0, 0});
g[x + 2 + 1].push_back({x + 3 + 1, 0, 0});
g[x + 3 + 1].push_back({x + 2 + 1, 0, 0});
}
}
}
char op1, op2;
int col1, col2;
cin >> op1 >> col1 >> op2 >> col2;
if (op1 == 'U' || op1 == 'D') {
sy = col1;
sx = (op1 == 'U' ? 1 : n);
} else {
sx = col1;
sy = (op1 == 'L' ? 1 : m);
}
if (op2 == 'U' || op2 == 'D') {
gy = col2;
gx = (op2 == 'U' ? 1 : n);
} else {
gx = col2;
gy = (op2 == 'L' ? 1 : m);
}
int S = 4 * (id(sx, sy) - 1) + mp[op1], T = 4 * (id(gx, gy) - 1) + mp[op2];
dijkstra(S);
if (dis[T] >= 9.9e8) return cout << "IMPOSSIBLE\n", void();
fr[S] = -4;
while (T > 0) {
int x = d_id(((T - 1) / 4) + 1).first;
int y = d_id(((T - 1) / 4) + 1).second;
int c = 0, cc = 0;
while (((fr[T] - 1) / 4) == ((T - 1) / 4)) {
if (typ[T]) c++;
else cc++;
T = fr[T];
}
if(c + cc != 2){
if(a[x][y] == '.'){
if(c == 1) a[x][y] = '\\';
if(cc == 1) a[x][y] = '/';
}
}
T = fr[T];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cout << a[i][j];
}
cout << "\n";
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
mp['U'] = 1, mp['L'] = 2, mp['D'] = 3, mp['R'] = 4;
for (cin >> T; T; T--) solve();
return 0;
}