QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#506120 | #6422. Evil Coordinate | Abcl | TL | 0ms | 3548kb | C++14 | 2.7kb | 2024-08-05 15:23:38 | 2024-08-05 15:23:38 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<char> ans;
int end_x,end_y;
unordered_map<char,int> mp;
int f=0;
int mx,my;
void dfs(int nowx,int nowy){
if(nowx==end_x&&nowy==end_y){
f=1;
return;
}
if(nowx+1!=mx&&nowy!=my&&mp['R']){
mp['R']--;
ans.emplace_back('R');
dfs(nowx+1,nowy);
mp['R']++;ans.pop_back();
if(f)return;
}
if(nowx-1!=mx&&nowy!=my&&mp['L']){
mp['L']--;
ans.emplace_back('L');
dfs(nowx-1,nowy);
mp['L']++;ans.pop_back();
if(f)return;
}
if(nowx!=mx&&nowy+1!=my&&mp['U']){
mp['U']--;
ans.emplace_back('U');
dfs(nowx,nowy+1);
mp['U']++;ans.pop_back();
if(f)return;
}
if(nowx!=mx&&nowy-1!=my&&mp['D']){
mp['D']--;
ans.emplace_back('D');
dfs(nowx,nowy-1);
mp['D']++;ans.pop_back();
if(f)return;
}
}
void solve() {
cin>>mx>>my;
string str;
cin>>str;
f=0;ans.clear();
if(mx==0&&my==0) {
cout<<"Impossible"<<endl;
return;
}
mp.clear();
for(int i=0; i<str.length(); i++) {
mp[str[i]]++;//统计各个方向移动的指令数目
}
end_x=mp['R']-mp['L'];//不要取绝对值就是算最后的坐标
end_y=mp['U']-mp['D'];
if(mx==end_x&&my==end_y){
cout<<"Impossible"<<endl;
return;
}
if(end_x!=mx&&end_y!=my) { //如果两个最终的结果都不跟坐标相同
dfs(0,0);
for(auto it:ans)cout<<it;
cout<<endl;
return;
}
else if(end_x!=mx&&end_y==my) {//若x不相同,但是y的坐标相同的话
if(mx>0&&end_x>mx){
cout<<"Impossible"<<endl;
return;
}
else if(mx<0&&end_x<mx){
cout<<"Impossible"<<endl;
return;
}
for(int i=1;i<=min(mp['L'],mp['R']); i++) {
cout<<'L'<<'R';
}
for(int i=min(mp['L'],mp['R'])+1; i<=max(mp['L'],mp['R']); i++) {
if(mp['L']>mp['R'])
cout<<'L';
else cout<<'R';
}
for(int i=0; i<mp['U']; i++) {
cout<<'U';
}
for(int i=0; i<mp['D']; i++) {
cout<<'D';
}
cout<<endl;
return;
}
else if(end_y!=my&&end_x==mx) { //如果y最后不相同但是x相同,
if(my>0&&end_y>my){
cout<<"Impossible"<<endl;
return;
}
else if(my<0&&end_y<my){
cout<<"Impossible"<<endl;
return;
}
for(int i=1; i<=min(mp['U'],mp['D']); i++) {
cout<<'U'<<'D';
}
for(int i=min(mp['U'],mp['D'])+1;i<=max(mp['U'],mp['D']); i++){
if(mp['U']>mp['D'])
cout<<'U';
else cout<<'D';
}
for(int i=0; i<mp['L']; i++) {
cout<<'L';
}
for(int i=0; i<mp['R']; i++) {
cout<<'R';
}
cout<<endl;
return;
}
}
signed main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t;
cin>>t;
while(t--) {
solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3548kb
input:
5 1 1 RURULLD 0 5 UUU 0 3 UUU 0 2 UUU 0 0 UUU
output:
LRLRUUD UUU Impossible Impossible Impossible
result:
ok 5 cases
Test #2:
score: -100
Time Limit Exceeded
input:
11109 6 0 RUDUDR 2 0 URU 0 0 UDRU 0 0 R -1 1 LDUUDDRUUL -1 5 RRUUUDUUU -8 4 RRDRLDR 2 0 UD 0 0 UUDD 3 -2 LDDLLLRR 3 -2 LDRURLDD 1 0 RRL -1 0 DUDDLLRDU -4 0 LL -1 -1 DLRLDLUDUR 1 4 URDULUR 0 0 DDUUDUDDDD 0 2 UU 1 0 RRULD 0 -2 LDLRLLDRRL 0 1 RLRLLRLUR -3 0 RL 0 0 D 0 0 L 0 0 DDLRRUDRUD 0 0 DULU 2 0 RR...
output:
RRUUDD Impossible Impossible Impossible RRUUUUUUD UD Impossible LRLRLLDD LRLRUDDD Impossible UDUDDDLLR LL Impossible UDUULRR Impossible Impossible Impossible LRLRLRLLDD Impossible LR Impossible Impossible Impossible Impossible Impossible LRLRLRRRUU Impossible LUUUDDD UDUDRR Impossible LRRUUDD L...