Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.63 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class FiveHundredEleven {
  4.  
  5.     private void go(int memory, int played){
  6.         if (memo[memory][played] != 0) {
  7.             return;
  8.         }
  9.         int remaining = initialState.length - played;
  10.         for (int current : initialState){
  11.  
  12.             if ((memory != (memory | current))){
  13.                 remaining--;
  14.                 if ((memory | current) == 511) {
  15.                     continue;
  16.                 }
  17.                 go(memory | current, played + 1);
  18.                 if (memo[memory | current][played + 1] == 1){
  19.                     memo[memory][played] = 2;
  20.                     return;
  21.                 }
  22.             }
  23.         }
  24.         if (remaining  !=  0){
  25.             go(memory, played + 1);
  26.             if (memo[memory][played + 1] == 1){
  27.                 memo[memory][played] = 2;
  28.                 return;
  29.             }
  30.         }
  31.         memo[memory][played] = 1;
  32.         return;
  33.     }
  34.  
  35.         public String theWinner(int[] cards) {
  36.                 int total = 0;
  37.         for (int cur: cards){
  38.             total |= cur;
  39.         }
  40.         if (total < 511){
  41.             return cards.length % 2 == 1 ? "Fox Ciel" : "Toastman";
  42.         }
  43.         initialState = cards.clone();
  44.         memo = new int [1 << 9][cards.length + 1];
  45.         go(0,0);
  46.         return memo[0][0] == 2? "Fox Ciel" : "Toastman";
  47.  
  48.         }
  49.     private static int []initialState;
  50.     private static int [][]memo;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement