//UVA10618TangoTangoInsurrection
#include<cstdio>
#include<cstring>
const int MAXN = 70 + 5;
int pos[256];
char seq[MAXN];
const int up = 0, down = 3, right = 2, left = 1;
const char operation[] = ".LR";
int action[MAXN][4][4][3], d[MAXN][4][4][3];
int energy(int a, int ta) {
if(a == ta) return 3;
else if(a + ta == 3) return 7;
return 5;
}
int energy(int i, int a, int b, int s, int f, int t, int& ta, int& tb) {
ta = a; tb = b;
if(f == 0) return 0;
if(f == 1) ta = t;
else if(f == 2) tb = t;
if(ta == tb) return -1;
if(ta == right && tb == left) return -1;
if(ta == right && tb != b) return -1;
if(tb == left && ta != a) return -1;
int e;
if(f != s) e = 1;
else if(f == 1) e = energy(a, ta);
else if(f == 2) e = energy(b, tb);
return e;
}
void update(int i, int a, int b, int s, int f, int t) {
int ta, tb;
int e = energy(i, a, b, s, f, t, ta, tb);
if(e < 0) return ;//非法的移动方案
int& ans = d[i][a][b][s];
int cost = d[i + 1][ta][tb][f] + e;
if(cost < ans) {
ans = cost;
action[i][a][b][s] = f * 4 + t;//编码本次移动方案
}
}
int main() {
pos['U'] = 0; pos['D'] = 3; pos['L'] = 1; pos['R'] = 2;
while(scanf("%s", seq) == 1) {
memset(d, 0, sizeof(d));
if(seq[0] == '#') break;
int n = strlen(seq);
for(int i = n - 1; i >= 0; i--)
for(int a = 0; a < 4; a++)
for(int b = 0; b < 4; b++)
if(a != b)
for(int s = 0; s < 3; s++) {
d[i][a][b][s] = 10 * n;
if(seq[i] == '.') {
update(i, a, b, s, 0, 0);//不动
for(int t = 0; t < 4; t++) {
update(i, a, b, s, 1, t);//动左脚到位置t
update(i, a, b, s, 2, t);//动右脚到位置t
}
}
else {
update(i, a, b, s, 1, pos[seq[i]]);
update(i, a, b, s, 2, pos[seq[i]]);
}
}
int a = left, b = right, s = 0;
for(int i = 0; i < n; i++) {
int t = action[i][a][b][s] % 4;
int f = action[i][a][b][s] / 4;
printf("%c", operation[f]);
s = f;
if(f == 1) a = t;//本步移动的是左脚,下同理
else if(f == 2) b = t;
}
printf("\n");
}
return 0;
}
/*
*/
UVA10618TangoTangoInsurrection
最新推荐文章于 2021-08-13 21:54:32 发布