CF Soldier and Cards (模拟)

Soldier and Cards
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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





纯链表模拟,怎么判断无解当时没想到,所以开了个很大的循环,如果这个循环内还没算出的话就无解。
  1 #include <iostream>
  2 #include <fstream>
  3 #include <cstdio>
  4 #include <string>
  5 #include <queue>
  6 #include <vector>
  7 #include <map>
  8 #include <algorithm>
  9 #include <cstring>
 10 #include <cctype>
 11 #include <cstdlib>
 12 #include <cmath>
 13 #include <ctime>
 14 using    namespace    std;
 15 
 16 struct    Node
 17 {
 18     int    num;
 19     Node    * next;
 20 };
 21 
 22 int    main(void)
 23 {
 24     Node    * first_1,* first_2,* last_1,* last_2;
 25     Node    * cur,* front;
 26     int    n,k_1,k_2,num,count = 0;
 27 
 28     scanf("%d",&n);
 29 
 30     scanf("%d",&k_1);
 31     for(int i = 0;i < k_1;i ++)
 32     {
 33         scanf("%d",&num);
 34         cur = new Node;
 35         cur -> num = num;
 36         cur -> next = nullptr;
 37         if(!i)
 38         {
 39             front = first_1 = cur;
 40             last_1 = cur;
 41             continue;
 42         }
 43         front -> next = cur;
 44         front = cur;
 45         last_1 = cur;
 46     }
 47 
 48     scanf("%d",&k_2);
 49     for(int i = 0;i < k_2;i ++)
 50     {
 51         scanf("%d",&num);
 52         cur = new Node;
 53         cur -> num = num;
 54         cur -> next = nullptr;
 55         if(!i)
 56         {
 57             front = first_2 = cur;
 58             last_2 = cur;
 59             continue;
 60         }
 61         front -> next = cur;
 62         front = cur;
 63         last_2 = cur;
 64     }
 65 
 66     while(first_1 && first_2)
 67     {
 68         Node    * temp_1 = first_1;
 69         Node    * temp_2 = first_2;
 70 
 71         if(temp_1 -> num < temp_2 -> num)
 72         {
 73             Node    * cur_1 = new    Node;
 74             Node    * cur_2 = new    Node;
 75             cur_1 -> num = temp_1 -> num;
 76             cur_2 -> num = temp_2 -> num;
 77 
 78             last_2 -> next = cur_1;
 79             cur_1 -> next = cur_2;
 80             cur_2 -> next = nullptr;
 81             last_2 = cur_2;
 82         }
 83         else
 84         {
 85             Node    * cur_1 = new    Node;
 86             Node    * cur_2 = new    Node;
 87             cur_1 -> num = temp_1 -> num;
 88             cur_2 -> num = temp_2 -> num;
 89 
 90             last_1 -> next = cur_2;
 91             cur_2 -> next = cur_1;
 92             cur_1 -> next = nullptr;
 93             last_1 = cur_1;
 94         }
 95 
 96         first_1 = first_1 -> next;
 97         first_2 = first_2 -> next;
 98         count ++;
 99         if(count > 500)
100         {
101             puts("-1");
102             return    0;
103         }
104     }
105     if(!first_1)
106         printf("%d 2
",count);
107     else
108         printf("%d 1
",count);
109 
110     return 0;
111 }
原文地址:https://www.cnblogs.com/xz816111/p/4532060.html