fork download
  1. import java.util.LinkedList;
  2. import java.util.Scanner;
  3. public class Main {
  4. public static void main(String[] args) {
  5. try (Scanner in = new Scanner(System.in)) {
  6. while (in.hasNext()) {
  7. LinkedList<Integer> p1 = new LinkedList<>();
  8. LinkedList<Integer> p2 = new LinkedList<>();
  9. try (Scanner line = new Scanner(in.nextLine())) {
  10. while (line.hasNext()) p1.add(line.nextInt());
  11. }
  12. try (Scanner line = new Scanner(in.nextLine())) {
  13. while (line.hasNext()) p2.add(line.nextInt());
  14. }
  15. System.out.println(play(p1, p2));
  16. }
  17. }
  18. }
  19. /**
  20. * Play a full game of war on two decks
  21. * @param a
  22. * @param b
  23. * @return
  24. */
  25. public static int play(LinkedList<Integer> a, LinkedList<Integer> b) {
  26. while (true) {
  27. //System.out.printf("%s%n%s%n%n",a,b);
  28. if (a.size() == 0 && b.size() == 0) return 0;
  29. else if (a.size() == 0) return 2;
  30. else if (b.size() == 0) return 1;
  31. int ax = a.removeFirst();
  32. int bx = b.removeFirst();
  33. if (ax>bx) { // check normal battle conditions
  34. a.addLast(ax);
  35. a.addLast(bx);
  36. } else if (bx>ax) {
  37. b.addLast(bx);
  38. b.addLast(ax);
  39. } else {
  40. int warResult = war(a, b); // ax == bx
  41. if (warResult >= 0) return warResult;
  42. else if (warResult == -1) {
  43. a.add(ax); // Don't forget to re-add the tied cards!
  44. a.add(bx);
  45. } else if (warResult == -2) {
  46. b.add(bx);
  47. b.add(ax);
  48. } else throw new RuntimeException("wtf");
  49. }
  50. }
  51. }
  52. /**
  53. * Execute a war (after a tied battle).
  54. * @param a player 1's deck (having removed tying cards if this is a top-level war)
  55. * @param b player 2's deck (having removed tying cards if this is a top-level war)
  56. * @return non-negative result indicates game-over, negative result indicates only this war's result
  57. */
  58. public static int war(LinkedList<Integer> a, LinkedList<Integer> b) {
  59. if (a.size() == 0 && b.size() == 0) return 0;
  60. else if (a.size() == 0) return 2;
  61. else if (b.size() == 0) return 1;
  62. LinkedList<Integer> aSoldier = new LinkedList<>();
  63. LinkedList<Integer> bSoldier = new LinkedList<>();
  64. int n = min(4,min(a.size(),b.size()))-1; //This variable used to be in the for loop declaration. Since a and b change sizes, this was causing bugs that ultimately led to cards being re-added in the wrong order.
  65. for (int i=0; i<n; ++i) {
  66. aSoldier.addLast(a.removeFirst());
  67. bSoldier.addLast(b.removeFirst());
  68. }
  69. int ax = a.removeFirst();
  70. int bx = b.removeFirst();
  71. int result = (ax>bx)?(-1):((bx>ax)?(-2):(war(a,b)));
  72. if (result >= 0) return result;
  73. int wx,lx;
  74. LinkedList<Integer> w,wSoldier,lSoldier;
  75. if (result == -1) {
  76. wx = ax; lx = bx;
  77. w = a;
  78. wSoldier = aSoldier;
  79. lSoldier = bSoldier;
  80. } else if (result == -2) {
  81. wx = bx; lx = ax;
  82. w = b;
  83. wSoldier = bSoldier;
  84. lSoldier = aSoldier;
  85. } else throw new RuntimeException("wtf - war");
  86. while (wSoldier.size() > 0) w.add(wSoldier.removeFirst());
  87. w.add(wx);
  88. while (lSoldier.size() > 0) w.add(lSoldier.removeFirst());
  89. w.add(lx);
  90. return result;
  91. }
  92. public static int min(int a, int b) {
  93. return ((a<b)?a:b);
  94. }
  95. }
Success #stdin #stdout 0.09s 4386816KB
stdin
5 1 13 10 11 3 2 10 4 12 5 11 10 5 7 6 6 11 9 6 3 13 6 1 8 1 
9 12 8 3 11 10 1 4 2 4 7 9 13 8 2 13 7 4 2 8 9 12 3 12 7 5 
3 11 6 12 2 13 5 7 10 3 10 4 12 11 1 13 12 2 1 7 10 6 12 5 8 1 
9 10 7 9 5 2 6 1 11 11 7 9 3 4 8 3 4 8 8 4 6 9 13 2 13 5 
1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 
1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13
stdout
2
2
0