-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotWalkProblem.java
More file actions
131 lines (81 loc) · 2.16 KB
/
RobotWalkProblem.java
File metadata and controls
131 lines (81 loc) · 2.16 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
public class RobotWalkProblem{
public static int robotWalk1(int N,int start,int end,int rest){
return process1(N,start,end,rest);
}
public static int process1(int N,int cur,int end,int rest){
if(rest ==0){
return cur == end ? 1 : 0;
}
else if(cur == 0){
return process1(N,1,end,rest-1);
}else
if(cur == N){
return process1(N,N-1,end,rest-1);
}else{
return process1(N,cur-1,end,rest-1)+process1(N,cur+1,end,rest-1);
}
}
public static int robotWalk2(int N,int start,int end,int rest){
int[][] dp=new int[N+1][rest+1];
for (int i=0;i<=N ;i++ ) {
for (int j=0; j<=rest; j++) {
dp[i][j]=-1;
}
}
return process2(N,start,end,rest,dp);
}
public static int process2(int N,int cur,int end,int rest,int[][] dp){
if(dp[cur][rest]!=-1){
return dp[cur][rest];
}
int ans=0;
if(rest ==0){
ans= cur == end ? 1 : 0;
}
else if(cur == 0){
ans= process2(N,1,end,rest-1,dp);
}else
if(cur == N){
ans= process2(N,N-1,end,rest-1,dp);
}else{
ans= process2(N,cur-1,end,rest-1,dp)+process2(N,cur+1,end,rest-1,dp);
}
dp[cur][rest]=ans;
return ans;
}
public static int robotWalk3(int N,int start,int end,int step){
int[][] dp=new int[N+1][step+1];
//这里初始化的是第一列
//则说明:
//在后面的双重循环中,都是按照列的顺序,从左往右初始化
for (int cur=0;cur<=N ;cur++ ) {
dp[cur][0]= cur==end?1:0;
}
//按照列的顺序,从左往右初始化,第0列已经初始化
//从第1列出发,将0-N行的第一列初始化,以此类推
for (int rest=1; rest<=step;rest++) {
for (int cur=0;cur<=N ;cur++ ) {
int ans=0;
if(cur == 0){
ans= dp[1][rest-1];
}else
if(cur == N){
ans= dp[N-1][rest-1];
}else{
ans= dp[cur-1][rest-1]+dp[cur+1][rest-1];
}
dp[cur][rest]=ans;
}
}
return dp[start][step];
}
public static void main(String[] args){
int N=4;
int start=2;
int end=4;
int rest=8;
System.out.println("count = "+robotWalk1(N,start,end,rest));
System.out.println("count = "+robotWalk2(N,start,end,rest));
System.out.println("count = "+robotWalk3(N,start,end,rest));
}
}