fork download
  1. import java.io.*;
  2. import java.text.DecimalFormat;
  3. import java.util.*;
  4. public class Main{
  5. static boolean gameOver;
  6. static boolean firstTurn ;
  7. public static void main(String[] args) throws Throwable {
  8. PrintWriter out = new PrintWriter(System.out);
  9. while(true)
  10. {
  11. String xx = br.readLine();
  12. if(xx.charAt(0)=='#')
  13. break;
  14. Deque<String> nonD = new LinkedList<String>();
  15. Deque<String> D = new LinkedList<String>();
  16. boolean lastD = true;
  17. for (int i = 0; i < 13; i++) {
  18. if(lastD)
  19. nonD.add(st.nextToken());
  20. else
  21. D.add(st.nextToken());
  22. lastD = !lastD;
  23. }
  24. for (int i = 0; i < 3; i++) {
  25.  
  26. st = new StringTokenizer(br.readLine());
  27. for (int j = 0; j < 13; j++)
  28. {
  29. if(lastD)
  30. nonD.add(st.nextToken());
  31. else
  32. D.add(st.nextToken());
  33. lastD = !lastD;
  34. }
  35. }
  36. Deque<String> heap = new LinkedList<String>();
  37. gameOver = false;
  38. firstTurn = true;
  39. boolean winner = true;
  40. while(true)
  41. {
  42. if(firstTurn)
  43. {
  44. if(nonD.isEmpty() )
  45. {
  46. winner = false;
  47. break;
  48. }
  49. if(gameOver)
  50. {
  51. if(nonD.isEmpty())
  52. winner = false;
  53. else
  54. winner = true;
  55.  
  56. break;
  57. }
  58. String card = nonD.pollLast();
  59. heap.add(card);
  60. firstTurn = false;
  61. switch(card.charAt(1))
  62. {
  63. case 'A':
  64. firstTurn = !firstTurn;
  65. simulate(D, nonD, heap, 4);
  66. break;
  67. case 'K':
  68. firstTurn = !firstTurn;
  69. simulate(D, nonD, heap, 3);
  70. break;
  71. case 'Q':
  72. firstTurn = !firstTurn;
  73. simulate(D, nonD, heap, 2);
  74. break;
  75. case 'J':
  76. firstTurn = !firstTurn;
  77. simulate(D, nonD, heap, 1);
  78. break;
  79. default:
  80. break;
  81. }
  82. }
  83. if(!firstTurn)
  84. {
  85. if(D.isEmpty() )
  86. {
  87. winner = true;
  88. break;
  89. }
  90. if(gameOver)
  91. {
  92. if(nonD.isEmpty())
  93. winner = false;
  94. else
  95. winner = true;
  96.  
  97. break;
  98. }
  99. firstTurn = true;
  100. String card = D.pollLast();
  101. heap.add(card);
  102. switch(card.charAt(1))
  103. {
  104. case 'A':
  105. firstTurn = !firstTurn;
  106. simulate(nonD, D, heap, 4);
  107. break;
  108. case 'K':
  109. firstTurn = !firstTurn;
  110. simulate(nonD, D, heap, 3);
  111. break;
  112. case 'Q':
  113. firstTurn = !firstTurn;
  114. simulate(nonD, D, heap, 2);
  115. break;
  116. case 'J':
  117. firstTurn = !firstTurn;
  118. simulate(nonD, D, heap, 1);
  119. break;
  120. default:
  121. break;
  122. }
  123. }
  124. }
  125. if(!winner)
  126. {
  127. String ans = (D.size()<10)? " "+D.size() : D.size()+"";
  128. out.append("1 "+ans+"\n");
  129. //out.flush();
  130. }
  131. else
  132. {
  133. String ans = (nonD.size()<10)? " "+nonD.size() :nonD.size()+"";
  134. out.append("2 "+ans+"\n");
  135. //out.flush();
  136. }
  137. }
  138. out.flush();
  139. }
  140. public static void simulate(Deque<String> first ,Deque<String> sec,Deque<String> heap,int counter)
  141. {
  142.  
  143. while(counter-->0)
  144. {
  145. if(first.isEmpty())
  146. {
  147. gameOver = true;
  148. return;
  149. }
  150. String card = first.pollLast();
  151. heap.add(card);
  152. switch (card.charAt(1)) {
  153. case 'A':
  154. firstTurn = !firstTurn;
  155. simulate(sec, first, heap, 4);
  156. return;
  157. case 'K':
  158. firstTurn = !firstTurn;
  159. simulate(sec, first, heap, 3);
  160. return;
  161. case 'Q':
  162. firstTurn = !firstTurn;
  163. simulate(sec, first, heap, 2);
  164. return;
  165. case 'J':
  166. firstTurn = !firstTurn;
  167. simulate(sec, first, heap, 1);
  168. return;
  169. default:
  170. break;
  171. }
  172. }
  173. takeHeap(sec, heap);
  174.  
  175. }
  176. public static void takeHeap(Deque<String>player,Deque<String>heap)
  177. {
  178. while(!heap.isEmpty())
  179. {
  180. player.offerFirst(heap.pollFirst());
  181. }
  182. }
  183. }
  184.  
Success #stdin #stdout 0.04s 711168KB
stdin
HA H3 H4 CA SK S5 C5 S6 C4 D5 H7 HJ HQ
D4 D7 SJ DT H6 S9 CT HK C8 C9 D6 CJ C6
S8 D8 C2 S2 S3 C7 H5 DJ S4 DQ DK D9 D3
H9 DA SA CK CQ C3 HT SQ H8 S7 ST H2 D2
#
stdout
1 44