码迷,mamicode.com
首页 > 其他好文 > 详细

打牌~~~

时间:2015-07-24 23:59:10      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:

题意:

 

Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all 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.

 

Input

4 
2 1 3
2 4 2
Output
6 2

技术分享

技术分享

思路:
用队列模拟两个玩家打牌,比较两个玩家最顶上的大小,一直到有玩家没牌了。还有一种就是平手,直接设置为循环1000次之后还没出结果就是循环。

源代码:
 1 #include<iostream>
 2 #include<queue>
 3 #include<cstring>
 4 using namespace std;
 5 queue<int>p1, p2;
 6 int main()
 7 {
 8     int t;
 9     cin >> t;
10     int count = 0;
11     int a1, a2;
12     int i = 0;
13     int n;
14     int  m;
15     cin >> n;
16     for (i = 0; i<n; i++)
17     {
18         cin >> a1;
19         p1.push(a1);
20     }
21     cin >> m;
22     for (i = 0; i<m; i++)
23     {
24         cin >> a2;
25         p2.push(a2);
26     }
27     while (1)
28     {
29 
30         if (p1.empty() || p2.empty())break;
31         int p = p1.front();
32         int q = p2.front();
33         p1.pop();
34         p2.pop();
35 
36         if (p > q)
37         {
38             p1.push(q);
39             p1.push(p);
40             
41             count++;
42         }
43         else
44         {
45             p2.push(p);
46             p2.push(q);
47             
48             count++;
49         }
50 
51         if (count > 1000)
52         {
53             count = -1;
54             break;
55 
56         }
57     }
58 
59     if (p1.empty() || p2.empty())
60     {
61 
62         cout << count << " " << (p1.empty() ? 2 : 1) << endl;
63     }
64     else
65     {
66         cout << "-1" << endl;
67     }
68 
69     return 0;
70 }

 

心得:

     这个题目主要是在找循环那里卡了很久。。。然后就找了下可爱的度娘,看别人用的>1e6.。。。结果自己也跟着用了一下,

后来提交时直接在第一个就超时了,过了很久很久(在提交了很多次都在第一个测试就超时了以后)。。。直接把while(t--)去掉,循环次数>1000竟然过了,偶然的发现。

也是够了。。。看来以后还得学着点。

 

 







打牌~~~

标签:

原文地址:http://www.cnblogs.com/Lynn0814/p/4675017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!