zoukankan      html  css  js  c++  java
  • Codeforces Round #304 (Div. 2)——Cdfs——Soldier and Cards

    Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to nall values are different. They divide cards between them in some manner, it's possible that they have different number of cards. Then they play a "war"-like card game.

    The rules are following. On each turn a fight happens. Each of them picks card from the top of his stack and puts on the table. The one whose card value is bigger wins this fight and takes both cards from the table to the bottom of his stack. More precisely, he first takes his opponent's card and puts to the bottom of his stack, and then he puts his card to the bottom of his stack. If after some turn one of the player's stack becomes empty, he loses and the other one wins.

    You have to calculate how many fights will happen and who will win the game, or state that game won't end.

    Input

    First line contains a single integer n (2 ≤ n ≤ 10), the number of cards.

    Second line contains integer k1 (1 ≤ k1 ≤ n - 1), the number of the first soldier's cards. Then follow k1 integers that are the values on the first soldier's cards, from top to bottom of his stack.

    Third line contains integer k2 (k1 + k2 = n), the number of the second soldier's cards. Then follow k2 integers that are the values on the second soldier's cards, from top to bottom of his stack.

    All card values are different.

    Output

    If somebody wins in this game, print 2 integers where the first one stands for the number of fights before end of game and the second one is 1 or 2 showing which player has won.

    If the game won't end and will continue forever output  - 1.

    Sample test(s)
    input
    4
    2 1 3
    2 4 2
    output
    6 2
    input
    3
    1 2
    2 1 3
    output
    -1
    Note

    First samp

    大意:模拟栈。简单dfs,当step大的时候输出-1就行

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int a1[11],a2[11];
    int n,m;
    int dfs(int x1,int x2,int step)
    {
         if(step >= 10000){
            printf("-1
    ");
            return 0;
        }
        
        if(x1 == 0){
            printf("%d 2
    ",step);
            return 0;
        }
        if(x2 == 0){
            printf("%d 1
    ",step);
            return 0;
        }
        if(a1[x1] < a2[x2]){
            int temp = a2[x2];
            for(int i = x2-1; i >= 1; i--)
                a2[i+2] = a2[i];
            a2[2] = a1[x1];
            a2[1] = temp;
            dfs(--x1,++x2,step+1);
        }
        else {
            int temp = a1[x1];
            for(int i = x1-1 ; i >= 1; i--)
                a1[i+2] = a1[i];
            a1[2] = a2[x2];
            a1[1] = temp;
            dfs(++x1,--x2,step+1);
        }
       
    }
    int main()
    {
        int T;
        scanf("%d",&T);
            scanf("%d",&n);
            for(int i = n; i >= 1 ;i--)
                scanf("%d",&a1[i]);
            scanf("%d",&m);
            for(int i = m; i >= 1 ;i--)
                scanf("%d",&a2[i]);
            dfs(n,m,0);
        return 0;
    }
    

      

  • 相关阅读:
    android如何从网络中获取数据
    如何写好代码
    如何有效遍历集合中的对象
    Android中自定义控件
    Android如何从网络中获取图片
    如果对象的类型为T1,就做某件事;如果对象的类型为T2,就做另外一件事,请赏自己一个巴掌
    android如何在网络中获取数据
    android shell脚本使用
    ASP.NET应用程序
    php记住用户名和密码实现代码(cookie)
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4531326.html
Copyright © 2011-2022 走看看