2020ICPC南京 E.Evil Coordinate(构造)

题意:

有一个二维坐标系,
你开始你在(0,0)点,给定坐标(mx,my),
给定长度为n的字符串,只包含LRUD,对应上下左右,
现在你可以重排这个字符串,要求你按照重排的字符串走,
走的时候不能碰到(mx,my)点,
如果有解输出重排之后的字符串,
如果无解输出Impossible.

数据范围:n<=1e6,-1e9<=mx,my<=1e9

解法:
可以证明一定存在一种答案,使得相同的方向是连续排在一起的。
枚举 UDLR 的 24 种排列即可。
code:
#include <bits/stdc++.h>
using namespace std;
const int maxm=1e6+5;
int dx[]={-1,1,0,0};
int dy[]={0,0,1,-1};
string dir="LRUD";
char s[maxm];
int mx,my;
int cnt[4];
int a[4];
int n;
bool check(){
    int x=0,y=0;
    for(int i=0;i<4;i++){
        for(int j=1;j<=cnt[a[i]];j++){
            x+=dx[a[i]];
            y+=dy[a[i]];
            if(x==mx&&y==my)return 0;
        }
    }
    return 1;
}
signed main(){
    int T;cin>>T;
    while(T--){
        scanf("%d%d",&mx,&my);
        scanf("%s",s+1);
        if(mx==0&&my==0){
            puts("Impossible");
            continue;
        }
        n=strlen(s+1);
        for(int i=0;i<4;i++)cnt[i]=0;
        for(int i=1;i<=n;i++){
            if(s[i]=='L')cnt[0]++;//l
            else if(s[i]=='R')cnt[1]++;//r
            else if(s[i]=='U')cnt[2]++;//u
            else if(s[i]=='D')cnt[3]++;//d
        }
        for(int i=0;i<4;i++)a[i]=i;
        int ok=0;
        do{
            if(check()){
                ok=1;
                break;
            }
        }while(next_permutation(a,a+4));
        if(ok){
            for(int i=0;i<4;i++){
                for(int j=1;j<=cnt[a[i]];j++){
                    printf("%c",dir[a[i]]);
                }
            }
            puts("");
        }else{
            puts("Impossible");
        }
    }
    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值