-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumOfPath.java
More file actions
95 lines (83 loc) · 2.63 KB
/
NumOfPath.java
File metadata and controls
95 lines (83 loc) · 2.63 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package _Misc;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
// https://www.luogu.com.cn/problem/P1002
public class NumOfPath {
public int countPath(int[] input) {
// 定义 dp
// dp[i][j]: i,j 位置到最右下角的方法数
int N = input[0] + 1;
int M = input[1] + 1;
int[][] dp = new int[N][M];
Set<Integer> set = getAllBlockPoints(input);
int index = (N-1)*M + M - 1;
dp[N-1][M-1] = set.contains(index)?0:1;
// 填倒数第1行
for (int j = M - 2; j>=0; j--) {
index = (N-1)*M + j;
boolean flag = set.contains(index);
dp[N-1][j] = flag? 0:dp[N-1][j+1];
}
// 填倒数第1列
for (int i = N - 2; i>=0; i--) {
index = i*M + M-1;
dp[i][M-1] = set.contains(index)? 0:dp[i+1][M-1];
}
for (int j = M - 2; j>=0; j--) {
for (int i = N - 2; i>=0; i--) {
index = i*M + j;
if (set.contains(index)) {
dp[i][j] = 0;
} else {
dp[i][j] = dp[i+1][j] + dp[i][j+1];
}
}
}
return dp[0][0];
}
public static class Point {
private int x;
private int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
// 根据马点计算出所有 block 点
private Set<Integer> getAllBlockPoints(int[] input) {
// B 点 input[0], input[1]
// 马 点 input[2], input[3]
int N = input[0] + 1;
int M = input[1] + 1;
int x = input[2];
int y = input[3];
List<Point> list = new ArrayList<>();
list.add(new Point(x,y));
list.add(new Point(x-2,y-1));
list.add(new Point(x-2,y+1));
list.add(new Point(x-1,y+2));
list.add(new Point(x+1,y+2));
list.add(new Point(x+2,y+1));
list.add(new Point(x+2,y-1));
list.add(new Point(x+1,y-2));
list.add(new Point(x-1,y-2));
Set<Integer> ans = new HashSet<>();
for (Point p: list) {
if (p.x >=0 && p.x <N && p.y >=0 && p.y <M) {
// 二维坐标转换为一维坐标
int idx = p.x * M + p.y;
ans.add(idx);
}
}
return ans;
}
public static void main(String[] args) {
// int[] input = new int[]{6, 6, 3, 3};
int[] input = new int[]{8, 6, 0, 4};
NumOfPath sl = new NumOfPath();
int ans = sl.countPath(input);
System.out.println(ans);
}
}