Card Games (queue)

Source: Internet
Author: User

Card Game Problem Solving report

Before you write a question, talk about the queue.

Queue: "FIFO", also known as the Fair queue. Note: The queue does not require a defined size.

Header file:<queue> definition/declaration mode:queue<int> s;

Push (): queue, Pop (): Out

Front (): Takes the first element of the queue, but does not delete it, returns the element within the

Back (): Returns the last element in the queue

Topic:

Description

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

The rules are following. On each turn a fight happens. Each of the 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 B Ottom of his stack. If after some turn one of the player ' s stacks becomes empty, he loses and the other one wins.

Calculate how many fights would happen and who'll 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 Soldi Er ' s cards. Then follow k1 integers that is the values on the first soldier ' s cards, from top to bottom of his stack .

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

All card values is 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 would 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

Sample Output

Hint

First Sample:

Second Sample:

Main topic:

Two people play cards, the total number of cards not more than 10 cards, the face value of the card is different. In order to play cards, and then compare the size of the big side win, get the other side of the card, and the two cards are put to the end of the team, has been doing so, know that one hand without a card, no card of the party lost the game. If the game does not end, output-1; If the game is over, output the number of games played before the end of the game and the winning person.

Analysis:

It's easy to see that this is a problem with queues. Set up two queues and put the cards into their respective queues, each time the first element of the queue is removed and the size is compared, the winning person puts all two cards into the tail of the team, which is recorded in ans with a ans++. There is no end to ans=-1.

Code:

1 #include <cstdio>2 #include <queue>3UsingNamespaceStd4IntMain ()5{6 queue<Int>Q1,Q2;7IntN,k1,k2,a;8while (scanf ("%d", &n)! =EOF)9{Ten scanf ("%d",&K1);11Forint i=0;i<k1;i++)12{scanf ("%d",&a);14Q1.push (a);1516}scanf ("%d",&K2);18Forint j=0;j<k2;j++)19{scanf ("%d",&a);21stQ2.push (a);2223}24int ans=0;25While1)26{27if (Q1.empty () | | | Q2.empty ())Break;28int U=q1.front (), v=Q2.front ();29Q1.pop ();30Q2.pop ();31if (u>V32{33Q1.push (v);34Q1.push (U);35}36Else37{38Q2.push (U);39Q2.push (v);40}ans++;42if (ans>1E6)43{ans=-1;45Break;46}4748}49 printf (" %d "if (Ans!=-151 printf ( "%d\n" Q1.empty ()? 2:152 }53 return 0; The                 

Experience:

Stacks of queues have many similarities and should be compared to learn. Look at other people's code, found that they still have a lot of shortcomings, to work hard to correct, good study.

Card Games (queue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.