Evil Coordinate
亞洲區域賽(南京)
你好! 我是一個剛剛接觸acm的小菜雞,今天對南京站的E有一點想法,寫在這里和大家分享一下,
由于自己的代碼能力比較弱,所以寫的比較繁瑣,歡迎大家優化,
首先貼上原題:
鏈接:https://ac.nowcoder.com/acm/contest/10272/E



樣例:
input:
5
1 1
RURULLD
0 5
UUU
0 3
UUU
0 2
UUU
0 0
UUU
output:
LDLRUUR
UUU
Impossible
Impossible
Impossible
根據上面的題意內容可以知道,題目的大概意思就是給你一個坐標點(代表洞的位置),然后給你一串字串,然后讓你改變字串的順序,讓我們改變順序后,該機器人不經過洞的的坐標點即可,如果怎么都必須經過這個坐標點,那么就輸出Impossible,否則則輸出一個修改后的順序,
首先我們先根據字串確定出終點的坐標,然后用一個陣列記錄一下 上,下,左,右各走了多少步,
情況一:
顯然當這個洞的坐標如果在起始點和終點的話,那么一定就要輸出“Impossible”
情況二:
假如說終點的坐標不在坐標軸上:

如果那個洞在第 1 條道路上(即先在y軸上運動,然后在水平運動),那么我們就讓機器人走第二條線,
那么顯然如果那個洞在 2 第條道路上,我們就讓機器人走第一條道路嘍,
情況三:

第三種情況就是如果這個點在x軸或者y軸,但是終點在這個洞的靠外的一側,這樣的話我們就需要判斷一下
假設是在x軸上,那么我們就需要判斷一下,是否又在y軸方向的運動了,如果沒有的話,那么我們就直接輸出“Impossible”如果有的話,那么我們就先向y的方向行走,然后再沿著水平方向行走完,然后再在豎直方向行走回來,
如果實在y軸上那么也是這樣的一個道理,
情況四:

第四種情況就是如果終點在洞的內側,那么我們就不用這么麻煩了,我們需要做的就是現在豎直方向上運動,然后再想洞的相反的方向運動,比如說上圖我們就向左側運動,然后回到終點就可以了,
如果在y軸上操作也是一樣的,
接來下看一下弱弱的代碼,
建議大家可以根據上面的思路,自己去寫,(因為我寫的不好)
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<cstdio>
#include<stack>
#include<set>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdlib>
#include<map>
#include<cmath>
#define ios std::ios::sync_with_stdio(0)
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
const int N = 1e5+10;
char a[N];
int t;
int main(){
cin >> t;
while(t--){
int fx,fy;
cin >> fx >> fy;
int cn[5];
memset(a,0,sizeof(a));
memset(cn,0,sizeof(cn));
scanf("%s",a+1);
int len = strlen(a+1);
int sx = 0,sy = 0;
for(int i = 1 ; i <= len ; i++){
if(a[i] == 'L'){
sx--;
cn[1]++;
}
else if(a[i] == 'R'){
sx++;
cn[2]++;
}
else if(a[i] == 'U'){
sy++;
cn[3]++;
}
else if(a[i] == 'D'){
sy--;
cn[4]++;
}
}
if( (sx == fx && sy == fy) || (fx == 0 && fy == 0)){
cout << "Impossible" << endl;
}
else if(sx == fx && sy != fy){
if(sx == 0 && cn[1] == 0 && cn[2] == 0){
if(sy >= fy && sy >= 0 && fy >= 0){
cout <<"Impossible" << endl;
}
else if(sy <= fy && sy <= 0 && fy <= 0){
cout <<"Impossible" << endl;
}
else if(fy > 0){
for(int i = 1 ; i <= cn[4] ; i++){
cout << 'D';
}
for(int i = 1 ; i <= cn[3] ; i++){
cout << 'U';
}
cout << endl;
}
else if(fy < 0){
for(int i = 1 ; i <= cn[3] ; i++){
cout << 'U';
}
for(int i = 1 ; i <= cn[4] ; i++){
cout << 'D';
}
cout << endl;
}
}
else if(sx == 0){
if(cn[1]){
for(int i = 1 ; i <= cn[1] ; i++){
cout << 'L';
}
for(int i = 1 ; i <= cn[3] ; i++){
cout << 'U';
}
for(int i = 1 ; i <= cn[4] ; i++){
cout << 'D';
}
for(int i = 1 ; i <= cn[2] ; i++){
cout << 'R';
}
cout << endl;
}
}
else{
for(int i = 1 ; i <= cn[4] ; i++){
cout << 'D';
}
for(int i = 1 ; i <= cn[3] ; i++){
cout << 'U';
}
for(int i = 1 ; i <= cn[1] ; i++){
cout << 'L';
}
for(int i = 1 ; i <= cn[2] ; i++){
cout << 'R';
}
cout <<endl;
}
}
else if(sx != fx && sy == fy){
if(sy == 0 && cn[3] == 0 && cn[4] == 0){
if(sx >= fx && sx >= 0 && fx >= 0)
cout <<"Impossible" << endl;
else if(sx <= fx && sx <= 0 && fx <= 0){
cout <<"Impossible" << endl;
}
else if(fx > 0){
for(int i = 1 ; i <= cn[1] ; i++){
cout << 'L';
}
for(int i = 1 ; i <= cn[2] ; i++){
cout << 'R';
}
cout << endl;
}
else if(fx < 0){
for(int i = 1 ; i <= cn[2] ; i++){
cout << 'R';
}
for(int i = 1 ; i <= cn[1] ; i++){
cout << 'L';
}
cout << endl;
}
}
else if(sy == 0){
if(cn[3]){
for(int i = 1 ; i <= cn[3] ; i++){
cout << 'U';
}
for(int i = 1 ; i <= cn[1] ; i++){
cout << 'L';
}
for(int i = 1 ; i <= cn[2] ; i++){
cout << 'R';
}
for(int i = 1 ; i <= cn[4] ; i++){
cout << 'D';
}
cout << endl;
}
}
else{
for(int i = 1 ; i <= cn[1] ; i++){
cout << 'L';
}
for(int i = 1 ; i <= cn[2] ; i++){
cout << 'R';
}
for(int i = 1 ; i <= cn[4] ; i++){
cout << 'D';
}
for(int i = 1 ; i <= cn[3] ; i++){
cout << 'U';
}
cout << endl;
}
}
else{
if(fy == 0){
for(int i = 1 ; i <= cn[4] ; i++){
cout << 'D';
}
for(int i = 1 ; i <= cn[3] ; i++){
cout << 'U';
}
for(int i = 1 ; i <= cn[1] ; i++){
cout << 'L';
}
for(int i = 1 ; i <= cn[2] ; i++){
cout << 'R';
}
cout <<endl;
}
else{
for(int i = 1 ; i <= cn[1] ; i++){
cout << 'L';
}
for(int i = 1 ; i <= cn[2] ; i++){
cout << 'R';
}
for(int i = 1 ; i <= cn[4] ; i++){
cout << 'D';
}
for(int i = 1 ; i <= cn[3] ; i++){
cout << 'U';
}
cout <<endl;
}
}
}
return 0;
}
最后謝謝大家的閱讀,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/237979.html
標籤:其他
