Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Main {
- public static void main(String[] args) {
- // Uncomment this line if you want to read from a file
- In.open("public/test1.in");
- Out.compareTo("public/test1.out");
- int t = In.readInt();
- for (int i = 0; i < t; i++) {
- testCase();
- }
- // Uncomment this line if you want to read from a file
- In.close();
- }
- public static void testCase() {
- // Input using In.java class
- int w = In.readInt();
- int b = In.readInt();
- double[][] DP = new double[w+1][b+1]; // Chance of me winning if he starts with i white j black
- for (int i = 0; i <= w; i++) {
- for (int j = 0; j <= b; j++) {
- if (i == 0) {
- DP[i][j] = 1;
- } else {
- DP[i][j] = -1;
- }
- }
- }
- double res = solve(w, b, DP);
- boolean print = false;
- if (print) {
- for (int i = 0; i <= w; i++) {
- for (int j = 0; j <= b; j++) {
- System.out.print(String.format("|%5.2f", DP[i][j]));
- }
- System.out.print("|\n");
- }
- }
- Out.println(res);
- }
- public static double solve(int i, int j, double[][] DP) {
- float w = i; float b = j;
- if (i == 0) {
- return 1; // Instant win
- }
- if (i < 0 || j <= 0) {
- return 0; // Instant loose
- }
- if (DP[i][j] != -1) {
- return DP[i][j]; // Already calculated
- }
- double tot = w+b;
- double pOppNw = b/tot; // Prob opponent wins
- double pB = (b-1)/(tot-1); // Given opp. wins prob. that black drawn by "ghost"
- double pW = w/(tot-1); // Given opp. wins prob. that white drawn by "ghost"
- double pBs = (tot > 2) ? w/(tot-2) : 1; // Probability that I win after black ghost draw
- double pWs = (tot > 2) ? (w-1)/(tot-2) : 1; // Probability that I win after white ghost draw
- // Probability of win in next round (DP) when drawn 3 black (1 opp, 1 ghost, 1 me)
- double pBn = p(w,b-3,false)*solve(i,j-4,DP)+p(w,b-3,true)*solve(i-1,j-3,DP);
- // Probability of win in next round (DP) when drawn 2 black 1 white (black: 1 opp, 1 me; white: 1 ghost)
- 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);
- DP[i][j] = pOppNw*(pB*(pBs+(1-pBs)*pBn)+pW*(pWs+(1-pWs)*pWn));
- return DP[i][j];
- }
- public static double p(double w, double b, boolean white) {
- if (w <= 0 || b <= 0) {
- return 1;
- }
- return (white) ? w/(b+w) : b/(b+w);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement