第 45 屆國際大學生程式設計競賽(ICPC)亞洲區域賽(南京)簽到題E Evil Coordinate
技術標籤:演算法
problem
連結:https://ac.nowcoder.com/acm/contest/10272/E
來源:牛客網
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 262144K,其他語言524288K
Special Judge, 64bit IO Format: %lld
題目描述
A robot is standing on an infinite 2-dimensional plane. Programmed with a string s_1s_2\cdots s_ns
1
s
2
⋯s
n
of length nn, where s_i \in {\text{‘U’}, \text{‘D’}, \text{‘L’}, \text{‘R’}}s
∈{’U’,’D’,’L’,’R’}, the robot will start moving from (0, 0)(0,0) and will follow the instructions represented by the characters in the string.
More formally, let (x, y)(x,y) be the current coordinate of the robot. Starting from (0, 0)(0,0), the robot repeats the following procedure nn times. During the ii-th time:
i
=’U’ the robot moves from (x, y)(x,y) to (x, y+1)(x,y+1);
If s_i = \text{‘D’}s
i
=’D’ the robot moves from (x, y)(x,y) to (x, y-1)(x,y−1);
If s_i = \text{‘L’}s
i
=’L’ the robot moves from (x, y)(x,y) to (x-1, y)(x−1,y);
If s_i = \text{‘R’}s
i
=’R’ the robot moves from (x, y)(x,y) to (x+1, y)(x+1,y).
However, there is a mine buried under the coordinate (m_x, m_y)(m
x
,m
y
). If the robot steps onto (m_x, m_y)(m
x
,m
y
) during its movement, it will be blown up into pieces. Poor robot!
Your task is to rearrange the characters in the string in any order, so that the robot will not step onto (m_x, m_y)(m
x
,m
y
).
輸入描述:
There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:
The first line contains two integers m_xm
x
and m_ym
y
(-10^9 \le m_x, m_y \le 10^9−10
9
≤m
x
,m
y
≤10
9
) indicating the coordinate of the mine.
The second line contains a string s_1s_2\cdots s_ns
1
s
2
⋯s
n
of length nn (1 \le n \le 10^51≤n≤10
5
, s_i \in {\text{‘U’}, \text{‘D’}, \text{‘L’}, \text{‘R’}}s
i
∈{’U’,’D’,’L’,’R’}) indicating the string programmed into the robot.
It’s guaranteed that the sum of nn of all test cases will not exceed 10^610
6
.
輸出描述:
For each test case output one line. If a valid answer exists print the rearranged string, otherwise print “Impossible” (without quotes) instead. If there are multiple valid answers you can print any of them.
示例1
輸入
複製
5
1 1
RURULLD
0 5
UUU
0 3
UUU
0 2
UUU
0 0
UUU
輸出
複製
LDLRUUR
UUU
Impossible
Impossible
Impossible
solution
題意:
- 給出一個字串,UDLR分別表示向上下左右移動。預設在座標原點(0,0),求更改字元順序,讓其移動不經過題目地雷點(mx,my)。
思路:
- 可以證明存在一種答案,使得相同的方向是連續排在一起的,即列舉UDLR的24種排列即可。
程式碼:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const char a[]="UDLR";//用0123對應ULDR,a是字元,c是數量,q是排列。
const int dx[]={0,0,-1,1},dy[]={1,-1,0,0};
int main(){
//ios::sync_with_stdio(false); WA
int T; cin>>T;
while(T--){
int mx, my; cin>>mx>>my;
string s; cin>>s;
int c[4] = {};
for(int i=0;i<s.size();i++){
if(s[i]=='U')c[0]++;
if(s[i]=='D')c[1]++;
if(s[i]=='L')c[2]++;
if(s[i]=='R')c[3]++;
}
//for(int i =0; i<4;i++)cout<<c[i]<<"\n";
//起點或終點重合
int tx=c[3]-c[2],ty=c[0]-c[1];
if(tx==mx&&ty==my || mx==0&&my==0){
cout<<"Impossible\n";
continue;
}
int q[4]={0,1,2,3}, ook=0;
do{
int x=0,y=0,ok=1;
for(int i=0; i<=3; i++){//方向
for(int j=0;j<c[q[i]];j++){//移動(方向q[i]有長度c[q[i]])
x += dx[q[i]], y+=dy[q[i]];
if(x==mx&&y==my){
ok=0; break;
}
}
if(ok==0)break;
}
if(ok==1){//沒有雷就全方向輸出
for(int i=0; i<=3; i++)
for(int j=0; j<c[q[i]];j++)
putchar(a[q[i]]);
cout<<"\n";
ook=1;
break;
}
}while(next_permutation(q,q+4));
if(ook)continue;
cout<<"Impossible\n";
}
return 0;
}