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

      

  • 相关阅读:
    Flutter-路由
    写一个底部Tabs导航动态组件
    实例:Flutter布局01
    HTTP报文
    HTML文本的各种属性
    HTML必要简介和基础
    MySQL8.0.23安装超详细傻瓜式
    从一个HTTP请求来研究网络分层原理
    MySQL的增删改查
    MySQL常用数据类型有哪些?
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4531326.html
Copyright © 2011-2022 走看看