Evil Coordinate
可向四个方向移动,不能走到有地雷的位置,问有无可能,若有可能则输出任意一种可能的情况
题目链接
思路:一条道走到黑,因为任意一步都能进行平移,拼成一条道走到黑的情况。若不会走到地雷,则有24种RLUD全排列的情况,枚举每种情况即可。
stl种的next_permutation对数组进行全排列,数组初始设为0,1,2,3
#include <bits/stdc++.h>
using namespace std;
int x, y;
int a[4];
int u[4];
bool jd()
{
int dx = 0, dy = 0;
for(int i = 0; i < 4; i ++ ){
for(int j = 1; j <= a[u[i]]; j ++ ){
if(u[i] == 0) dx += 1;
else if(u[i] == 1) dx -= 1;
else if(u[i] == 2) dy += 1;
else dy -= 1;
if(dx == x && dy == y) return false;
}
}
return true;
}
int main()
{
int t;
scanf("%d", &t);
while(t -- )
{
cin >> x >> y;
string str; cin >> str;
for(int i = 0; i < 4; i ++ ) a[i] = 0;
int sum = 4;
for(int i = 0; i < str.size(); i ++ ){
if(str[i] == 'R') a[0] ++;
else if(str[i] == 'L') a[1] ++;
else if(str[i] == 'U') a[2] ++;
else a[3] ++;
}
int dx = a[0] - a[1], dy = a[2] - a[3];
if((dx == x && dy == y) || (x == 0 && y == 0)) {
cout << "Impossible" << endl;
continue;
}
else {
for(int i = 0; i < 4; i ++ ) u[i] = i;
bool flag = true;
for(int i = 0; i < 24; i ++ ){
// for(int i = 0; i < 4; i ++ )
// cout << u[i];
// cout << endl;
if(jd()){
for(int j = 0; j < 4; j ++ ){
for(int s = 0; s < a[u[j]]; s ++ ){
if(u[j] == 0) cout << "R";
else if(u[j] == 1) cout << "L";
else if(u[j] == 2) cout << "U";
else cout << "D";
}
}
flag = false;
break;
}
next_permutation(u, u + 4);//STL打法好
}
if(flag) cout << "Impossible" << endl;
else cout << endl;
}
}
return 0;
}