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

纸牌游戏(队列)

时间:2015-07-26 15:33:52      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

纸牌游戏解题报告

在写题目之前,还是要讲一下队列。

队列:“先进先出”,又称公平队列。注意:队列不需要定义大小。

头文件:<queue>   定义/声明方式:queue<int> s;

push():入队、     pop():出队  

front():取队首元素,但不删除,返回queue内的第一个元素

back():返回queue内的最后一个元素

 

题目:

Description

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  - 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:

技术分享

题目大意:

两人打牌,牌总数不超过10张,牌的面值也不同。按顺序出牌,然后比较大小,大的那方赢了,得到对方的牌,并把这两张牌都放到队尾,一直这样做下去,知道一方手中没有牌为止,没有牌的那方输掉比赛。如果比赛不能结束,输出-1;如果比赛结束,输出结束比赛前所玩游戏的次数和赢的人。

分析:

很容易看出来,这是一道关于队列的问题。建立两个队列并将牌放入各自队列中,每次取出队列的第一个元素并比较大小,赢的人把两个牌全都放入队尾,用ans记录,比一次ans++。没有结束ans=-1。

代码:

 1 #include<cstdio>
 2 #include<queue>
 3 using namespace std;
 4 int main()
 5 {
 6     queue<int>q1,q2;
 7     int n,k1,k2,a;
 8     while(scanf("%d",&n)!=EOF)
 9     {
10         scanf("%d",&k1);
11     for(int i=0;i<k1;i++)
12      {
13           scanf("%d",&a);
14           q1.push(a);
15 
16      }
17      scanf("%d",&k2);
18      for(int j=0;j<k2;j++)
19      {
20           scanf("%d",&a);
21           q2.push(a);
22 
23      }
24      int ans=0;
25      while(1)
26      {
27          if(q1.empty()||q2.empty())  break;
28          int u=q1.front(),v=q2.front();
29          q1.pop();
30          q2.pop();
31          if(u>v)
32          {
33                 q1.push(v);
34                 q1.push(u);
35          }
36          else
37         {
38             q2.push(u);
39             q2.push(v);
40          }
41          ans++;
42          if(ans>1e6)
43          {
44              ans=-1;
45              break;
46          }
47 
48      }
49          printf("%d",ans);
50          if(ans!=-1)
51             printf(" %d\n",q1.empty()?2:1);
52     }
53     return 0;
54 }

 

心得:

栈的队列有很多相似的地方,应该比较学习。看了别人的代码,发现自己还有很多不足,要努力改正,好好学习。

 

纸牌游戏(队列)

标签:

原文地址:http://www.cnblogs.com/ttmj865/p/4677715.html

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