-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPocketgems.java
More file actions
137 lines (76 loc) · 1.83 KB
/
Pocketgems.java
File metadata and controls
137 lines (76 loc) · 1.83 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
132
133
134
135
136
137
public class Pocketgems {
public static void main(String[] args){
for(int i=1;i<10; i++){
System.out.println(""+i+" :"+RobertReach(i));
}
}
public static boolean isLegal(int x, int y, int K){
int sum =0;
while(x!=0){
sum += x%10;
x = x/10;
}
while(y!=0){
sum += y%10;
y = y/10;
}
return sum<=K;
}
public static void dfs(boolean[][] visited, int x, int y, int K, int[] count){
visited[x][y] = true;
count[0]++;
if(x-1>=0&&visited[x-1][y]==false&&isLegal(x-1,y,K)){
dfs(visited,x-1,y,K, count);
}
if(y-1>=0&&visited[x][y-1]==false&&isLegal(x,y-1,K)){
dfs(visited,x,y-1,K, count);
}
if(x+1<visited[0].length&&visited[x+1][y]==false&&isLegal(x+1,y,K)){
dfs(visited,x+1,y,K, count);
}
if(y+1<visited.length&&visited[x][y+1]==false&&isLegal(x,y+1,K)){
dfs(visited,x,y+1,K, count);
}
}
public static int RobertReach(int K){
int[] count = new int[1];
count[0] = 0;
if(K == 0) return 0;
int bound = getbound(K);
boolean[][] visited = new boolean[bound][bound];
for(int i=0; i<visited.length;i++){
for(int j=0; j<visited[0].length;j++){
visited[i][j] = false;
}
}
dfs(visited,0,0,K,count);
return count[0];
}
public static int getbound(int K){
int digit = 1;
int sum = 0;
while(K>9){
sum+=9*digit;
digit*=10;
K -= 9;
}
sum+=K;
return sum;
}
/*
public int maxMyShare(int [] slices){
if (slices.length == 0) return 0;
int [] dp = new [slices.length+1][slices.length];
//i lenght, j start place
for(int i=0; i<slices.length; i++){
dp[0][i] = 0;
dp[1][i] = slices[i];
}
for(int i=2; i<dp.length; i++){
for(int j=0; j<slices.length;j++){
dp[i][j] = Math.max(sumofslices(slice,i,j)-dp[i-1][j+1], sumofslices(slice,i,j)-dp[i-1][j]);
}
}
}
*/
}