-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessMetric.java
More file actions
58 lines (48 loc) · 1.21 KB
/
ChessMetric.java
File metadata and controls
58 lines (48 loc) · 1.21 KB
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
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
public class ChessMetric {
static int fx, fy;
static int N, M;
static long memo[][][];
static int dr[] = {1, -1, 0, 0, 1, 2, 1, 2, -1, -2, -1, -2, 1, -1, 1, -1};
static int dc[] = {0 , 0, -1, 1, 2, 1, -2, -1, 2, 1, -2, -1, 1, -1, -1, 1};
// public static void main(String[] args) {
// System.out.println(howMany(3, new int[]{0, 0}, new int[]{0,0}, 2));
// }
public static long howMany(int size, int[] start, int[] end, int numMoves)
{
N = size;
M = numMoves;
fx = end[0]; fy = end[1];
memo = new long[N][N][M];
for(int i = 0; i < N; ++i)
for(int j = 0; j < N; ++j)
Arrays.fill(memo[i][j], -1);
return solve(start[0], start[1], 0);
}
private static long solve(int x, int y, int m) {
if(m > M) return 0;
if(m == M)
{
if(x == fx && y == fy)
return 1;
return 0;
}
if(memo[x][y][m] != -1) return memo[x][y][m];
long ways = 0;
int a, b;
for(int i = 0; i < 16; ++i)
{
a = x + dr[i];
b = y + dc[i];
if(!valid(a, b)) continue;
ways += solve(a, b, m + 1);
}
return memo[x][y][m] = ways;
}
static boolean valid(int x, int y)
{
return x >= 0 && y >= 0 && x < N && y < N;
}
}