• 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
    ");
                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
    ");
        }
        return 0;
    }
  • 相关阅读:
    14个你可能不知道的JavaScript调试技巧
    数据库设计四步骤
    mac 卸载 jdk
    node版本管理
    mysql order by limit 问题
    计算机一些基本概念的认识
    SQL设置主外键关联时报错
    阻止表单autocomplete
    常见字符编码
    编程语言分类
  • 原文地址:https://www.cnblogs.com/zzyh/p/14897512.html
Copyright © 2020-2023  润新知