[ICPC2020南京E] Evil Coordinate - 结论
Description
机器人从(0,0)出发,根据题目的输入字符串,到达终点,在已知的路径中会有一个地雷,要求在不改变字符串数量的前提下改变顺序,不踩到地雷。
Solution
只有一个地雷,我们将上下左右各自连在一起,那么4个方向的全排列对应最多4种解,一定包含了所有有解的情况
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int dx[5] = {0, -1, 1, 0, 0};
const int dy[5] = {0, 0, 0, 1, -1};
const char lut[5] = {' ', 'L', 'R', 'U', 'D'};
bool check(const vector<int> &seq, int mx, int my)
{
int x = 0, y = 0;
if (x == mx && y == my)
return false;
for (int i = 0; i < seq.size(); i++)
{
x += dx[seq[i]];
y += dy[seq[i]];
if (x == mx && y == my)
return false;
}
return true;
}
void solve()
{
int mx, my;
cin >> mx >> my;
string str;
cin >> str;
int n = str.length();
vector<int> seq(n);
for (int i = 0; i < n; i++)
{
if (str[i] == 'L')
seq[i] = 1;
if (str[i] == 'R')
seq[i] = 2;
if (str[i] == 'U')
seq[i] = 3;
if (str[i] == 'D')
seq[i] = 4;
}
vector<int> cnt(6);
for (int i = 0; i < n; i++)
cnt[seq[i]]++;
int a[4];
iota(a, a + 4, 1);
do
{
vector<int> tseq;
for (int i = 0; i < 4; i++)
{
int c = a[i];
for (int j = 0; j < cnt[c]; j++)
tseq.push_back(c);
}
// cout << "test ";
// for (int i = 0; i < n; i++)
// cout << lut[tseq[i]];
// cout << endl;
if (check(tseq, mx, my))
{
for (int i = 0; i < n; i++)
cout << lut[tseq[i]];
cout << endl;
return;
}
} while (next_permutation(a, a + 4));
cout << "Impossible" << endl;
}
signed main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--)
solve();
}