Pair Calculate(int u) { BigInteger ans; Pair temp; temp = new Pair(); if (g[u].size() == 0) { temp.x = BigInteger.ONE; temp.n = 1; return temp; } if (g[u].size() == 1) { temp = Calculate((int) g[u].get(0)); temp.n++; return temp; } int p, q; temp = Calculate((int) g[u].get(0)); ans = temp.x; p = temp.n; temp = Calculate((int) g[u].get(1)); ans = ans.multiply(temp.x); q = temp.n; ans = ans.multiply(C[p + q][p]); temp.x = ans; temp.n = p + q + 1; return temp; }
public void gameOfLife(int[][] board) { Set<Pair> set = new HashSet<>(); for (int i = 0; i < board.length; i++) for (int j = 0; j < board[i].length; j++) if (board[i][j] == 1) set.add(new Pair(i, j)); System.out.println(set); Pair tmp = new Pair(0, 0); for (int i = 0; i < board.length; i++) for (int j = 0; j < board[i].length; j++) { int cnt = 0; for (int k = 0; k < 8; k++) { int x = i + dx[k]; int y = j + dy[k]; tmp.x = x; tmp.y = y; if (set.contains(tmp)) cnt++; } if (board[i][j] == 1 && 2 <= cnt && cnt <= 3 || board[i][j] == 0 && cnt == 3) board[i][j] = 1; else board[i][j] = 0; } }