IPCP2020南京 E Evil Coordinate

传送门

题目大意:给出一个点(x,y),给出移动的顺序包含LRUD的一个字符串s,问是否存在s的一个排列,能在方格移动时不经过(x,y)。其实只要猜到 U D L R,相同字母都挨着,然后全排列枚举。

(出了个BUG,没理解next_permutation.

#include<iostream>
#include<map>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

char s[100002];
map<char,int>cnt;
map<int,char>a;
int c[5];


int main()
{
    int t;
    scanf("%d",&t);
    a[1]='R';a[2]='U';a[3]='L';a[4]='D';
    while(t--)
    {
        int x,y;
        cnt.clear();
        for(int i=1;i<=4;i++) c[i]=i;//!!!!(一开始写在while外面了一直WA)
        scanf("%d%d",&x,&y);
        scanf("%s",s+1);
        int len=strlen(s+1);
        if(x==0&&y==0)
        {
            printf("Impossible\n");
            continue;
        }
        for(int i=1;i<=len;i++)
        {
            cnt[s[i]]++;
        }
        bool ok=false;
        do
        {
            bool flag=true;
            int nowx=0,nowy=0;
            for(int i=1;i<=4;i++)
            {
                char ch=a[c[i]];
                int js=cnt[ch];
                for(int j=1;j<=js;j++)
                {
                    if(ch=='U') nowy++;
                    if(ch=='D') nowy--;
                    if(ch=='L') nowx--;
                    if(ch=='R') nowx++;
                    if(nowx==x&&nowy==y)
                    {
                        flag=false;
                        break;
                    }
                }
                if(!flag) break;
            }
            if(flag)
            {
                ok=true;
                for(int i=1;i<=4;i++)
                {
                    char ch=a[c[i]];
                    int js=cnt[ch];
                    for(int j=1;j<=js;j++)
                    {
                        cout<<ch;
                    }
                }
                cout<<endl;
                break;
            }
        }while(next_permutation(c+1,c+4+1));
        if(!ok) printf("Impossible\n");
    }
    return 0;
}

 

posted @ 2021-06-17 22:11  ANhour  阅读(129)  评论(0编辑  收藏  举报