Advertisement
SilvanM

Vanishing Bag - Solution

Aug 9th, 2023 (edited)
1,104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class Main {
  4.     public static void main(String[] args) {
  5.         // Uncomment this line if you want to read from a file
  6.         In.open("public/test1.in");
  7.         Out.compareTo("public/test1.out");
  8.        
  9.         int t = In.readInt();
  10.         for (int i = 0; i < t; i++) {
  11.             testCase();
  12.         }
  13.        
  14.         // Uncomment this line if you want to read from a file
  15.         In.close();
  16.     }
  17.  
  18.     public static void testCase() {
  19.         // Input using In.java class
  20.         int w = In.readInt();
  21.         int b = In.readInt();
  22.        
  23.         double[][] DP = new double[w+1][b+1]; // Chance of me winning if he starts with i white j black
  24.         for (int i = 0; i <= w; i++) {
  25.           for (int j = 0; j <= b; j++) {
  26.             if (i == 0) {
  27.               DP[i][j] = 1;
  28.             } else {
  29.               DP[i][j] = -1;
  30.             }
  31.           }
  32.         }
  33.        
  34.         double res = solve(w, b, DP);
  35.        
  36.         boolean print = false;
  37.         if (print) {
  38.           for (int i = 0; i <= w; i++) {
  39.             for (int j = 0; j <= b; j++) {
  40.               System.out.print(String.format("|%5.2f", DP[i][j]));
  41.             }
  42.             System.out.print("|\n");
  43.           }
  44.         }
  45.        
  46.         Out.println(res);
  47.     }
  48.    
  49.     public static double solve(int i, int j, double[][] DP) {
  50.       float w = i; float b = j;
  51.      
  52.       if (i == 0) {
  53.         return 1; // Instant win
  54.       }
  55.       if (i < 0 || j <= 0) {
  56.         return 0; // Instant loose
  57.       }
  58.      
  59.       if (DP[i][j] != -1) {
  60.         return DP[i][j]; // Already calculated
  61.       }
  62.      
  63.       double tot = w+b;
  64.      
  65.       double pOppNw = b/tot; // Prob opponent wins
  66.       double pB = (b-1)/(tot-1); // Given opp. wins prob. that black drawn by "ghost"
  67.       double pW = w/(tot-1); // Given opp. wins prob. that white drawn by "ghost"
  68.       double pBs = (tot > 2) ? w/(tot-2) : 1; // Probability that I win after black ghost draw
  69.       double pWs = (tot > 2) ? (w-1)/(tot-2) : 1; // Probability that I win after white ghost draw
  70.      
  71.       // Probability of win in next round (DP) when drawn 3 black (1 opp, 1 ghost, 1 me)
  72.       double pBn = p(w,b-3,false)*solve(i,j-4,DP)+p(w,b-3,true)*solve(i-1,j-3,DP);
  73.       // Probability of win in next round (DP) when drawn 2 black 1 white (black: 1 opp, 1 me; white: 1 ghost)
  74.       double pWn = p(w-1,b-2,false)*solve(i-1,j-3,DP)+p(w-1,b-2,true)*solve(i-2,j-2,DP);
  75.      
  76.       DP[i][j] = pOppNw*(pB*(pBs+(1-pBs)*pBn)+pW*(pWs+(1-pWs)*pWn));
  77.      
  78.       return DP[i][j];
  79.     }
  80.    
  81.     public static double p(double w, double b, boolean white) {
  82.       if (w <= 0 || b <= 0) {
  83.         return 1;
  84.       }
  85.       return (white) ? w/(b+w) : b/(b+w);
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement