forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_688.java
75 lines (70 loc) · 2.72 KB
/
_688.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.fishercoder.solutions;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class _688 {
public static class Solution1 {
/**
* This BFS solution results in TLE on Leetcode.
*/
public double knightProbability(int N, int K, int r, int c) {
int[][] directions = {{-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}};
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{r, c});
int level = K;
while (level-- > 0) {
int size = queue.size();
for (int i = 0; i < size; i++) {
int[] curr = queue.poll();
for (int[] direction : directions) {
int x = curr[0] + direction[0];
int y = curr[1] + direction[1];
if (x >= 0 && x < N && y >= 0 && y < N) {
queue.offer(new int[]{x, y});
}
}
}
}
double prob = queue.size();
for (int i = 0; i < K; i++) {
prob /= 8;
}
return prob;
}
}
public static class Solution2 {
/**
* Let f[r][c][k] mean the probability that the knight is still on board after k steps,
* we can deduce a recursion from its k-1 steps
* In addition, instead of using a 3-d array, we can only keep the most recent two layers,
* i.e. using only two 2-d arrays.
*/
public double knightProbability(int N, int K, int r, int c) {
int[][] directions = {{-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}};
double[][] dp0 = new double[N][N];
for (double[] row : dp0) {
Arrays.fill(row, 1);
}
for (int k = 0; k < K; k++) {
double[][] dp1 = new double[N][N];
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int l = 0; l < directions.length; l++) {
int[] direction = directions[l];
int x = i + direction[0];
int y = j + direction[1];
if (x >= 0 && y >= 0 && x < N && y < N) {
dp1[i][j] += dp0[x][y];
}
}
}
}
dp0 = dp1;
}
return dp0[r][c] / Math.pow(8, K);
}
}
public static void main(String... args) {
System.out.println((double) 2 / 8);
}
}