zoukankan      html  css  js  c++  java
  • Codeforces546C:Soldier and Cards 解题心得

    原题:

    Description

    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 Input

    Input
    4
    2 1 3
    2 4 2
    Output
    6 2
    Input
    3
    1 2
    2 1 3
    Output
    -1

    Hint

    First sample:

    Second sample:

     
     
     
    我的代码:
     1 #include<iostream>
     2 #include<set>
     3 #include<queue>
     4 
     5 
     6 using namespace std;
     7 #define  di queue<int>
     8 
     9 di card1;
    10 di card2;
    11 int k1[12];
    12 int k2[12];
    13 set<di> vis;
    14 
    15 void move(di &a,di&b)
    16 {
    17     if (a.front() > b.front())
    18     {
    19         a.push(b.front());
    20         b.pop();
    21         a.push(a.front());
    22         a.pop();
    23     }
    24     else
    25     {
    26         b.push(a.front());
    27         a.pop();
    28         b.push(b.front());
    29         b.pop();
    30     }
    31 }
    32 
    33 
    34 
    35 int main()
    36 {
    37 
    38     int n, n1, n2;
    39     cin >> n;
    40     cin >> n1;
    41     for (int i = 0; i < n1; i++)
    42     {
    43         cin >> k1[i];
    44         card1.push(k1[i]);
    45     }
    46     cin >> n2;
    47     for (int i = 0; i < n2; i++)
    48     {
    49         cin >> k2[i];
    50         card2.push(k2[i]);
    51     }
    52 
    53     int cycle = 0;
    54     int win = 0;
    55     int ans = 0;
    56     while (1)
    57     {
    58         if (ans == 1e4)
    59         {
    60             cycle = 1;
    61             ans = -1;
    62             break;
    63         }
    64         move(card1, card2);
    65         ans++;
    66         if (card1.empty() )
    67         {
    68             cycle = 0;
    69             win = 2;
    70             break;
    71         }
    72         if (card2.empty())
    73         {
    74             cycle = 0;
    75             win = 1;
    76             break;
    77         }
    78     }
    79     if (cycle)
    80     {
    81         cout << ans << endl;
    82     }
    83     else
    84     {
    85         cout << ans <<' '<<win<< endl;
    86     }
    87     return 0;
    88 }
    View Code
  • 相关阅读:
    Iframe和Frame中实现cookie跨域的方法(转载)
    android中拷贝assets下的资源文件到SD卡中(可以超过1M)
    OpenSL ES 查询设备支持的SL Profiles
    NDK开发中的一个HTTP下载实例附带下载进度
    android中配置文件property的用途以及使用<转>
    Eclipse 工程使用相对路径导入Jar包设置
    Android 解压zip文件(支持中文)
    c++实现一个比较两个string类型的版本号的小demo
    linux c++下载http文件并显示进度<转>
    Linux下类似windows下_beginthread和_endthread 的多线程开发
  • 原文地址:https://www.cnblogs.com/shawn-ji/p/4678398.html
Copyright © 2011-2022 走看看