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;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章