zoukankan      html  css  js  c++  java
  • URAL1501——DFS——Sense of Beauty

    Description

    The owner of a casino for New Russians has a very refined sense of beauty. For example, after a game there remain two piles with the same number of cards on the table, and the owner likes the cards to be arranged into two piles according to the color: one pile with red cards and the other with black cards. Of course, this is done not by the owner himself, but by a croupier. The owner just likes to watch the process. The croupier takes a card from the top of one of the initial piles and puts it into one of the new piles; this is repeated until all the cards from the initial piles are transferred. The owner doesn't like it if one of the resulting piles grows faster than the other. At each moment the resulting piles must not differ in size by more than one card; a bigger difference would contradict the owner's sense of beauty. Help the croupier to arrange the cards according to the tastes of his owner.

    Input

    The first line of the input contains the number N of cards in each of the piles (4 ≤ N ≤ 1000). Each of the next two lines contains N digits 0 or 1 describing the piles: 1 denotes a red-suit card and 0 denotes a black-suit card. The cards in a pile are described from the top to the bottom. There are in total N red and N black cards in the two piles.

    Output

    Output a line containing 2 N digits 1 or 2, which describes the process of transferring the cards. Each number shows the number of the pile from which a card is taken. If it is impossible to perform this task according to the given rules, output "Impossible".

    Sample Input

    inputoutput
    4
    0011
    0110
    
    22121112
    
    4
    1100
    1100
    
    Impossible
    

    大意:有两组牌,每组只能从前往后拿,1表示红,0表示黑,要求拿出来的牌的顺序不能使得两种相同颜色的在一起,dfs大发好~~是时候去学一波了。感觉用来遍历所有的情况好方便    原文题解

    就是不知道怎么多组输入。。最后一个在哪里结束

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    bool dp[1010][1010];
    char a[1010],b[1010];
    bool dfs(int i,int j)
    {
        if(!i&&!j)
            return true;
        if(dp[i][j] == false)//如果没有访问过
            dp[i][j] = true;
        else return false;
        if(i >= 2 && a[i]!=a[i-1]&&dfs(i-2,j)){
            printf("11");
            return true;
        }
        if(j >= 2 && b[j]!=b[j-1] && dfs(i,j-2)){
            printf("22");
            return true;
        }
        if(i&&j&&a[i]!=b[j]&&dfs(i-1,j-1)){
            printf("12");
            return true;
        }
        return false ;
    }
    int main()
    {
        int n;
         scanf("%d%s%s",&n,a+1,b+1);
            memset(dp,false,sizeof(dp));
            if(!dfs(n,n))
                printf("Impossible
    ");
        return 0;
    }
    

      

  • 相关阅读:
    2019牛客暑期多校训练营(第二场)H Second Large Rectangle
    HDU -1506 Largest Rectangle in a Histogram&&51nod 1158 全是1的最大子矩阵 (单调栈)
    吉哥系列故事——完美队形II(马拉车算法)
    Theme Section
    激光雷达和毫米波雷达
    模型压缩95%:Lite Transformer,MIT韩松等人
    分布式深度学习DDL解析
    TOF摄像机可以替代Flash激光雷达吗?
    毫米波雷达分类和技术方案
    高精地图与自动驾驶(下)
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4497245.html
Copyright © 2011-2022 走看看