-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhopscotch.java
67 lines (54 loc) · 2.18 KB
/
hopscotch.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
import java.io.*;
import java.util.*;
public class hopscotch {
public static void dp(int x, int y, int[][] dp,boolean[][] visited, int[][] keys, int r, int c){
int curkey = keys[x][y];
//get what you are currently standing on
int count = 0;
//count is the summation of all the pathes
for(int i = x+1; i < r-1; i++) {
for(int j = y+1; j < c-1; j++) {
//go through the next right column to the end and the next down column to the bottom
if(keys[i][j] != curkey){
//if you find a key that isn't equal to the one you're standing on then you add it to the total count
if(!visited[i][j]) {
//if you haven't
dp(i,j,dp,visited,keys,r,c);
visited[i][j] = true;
}
count += dp[i][j];
count %=1000000007;
}
}
}
if(keys[r-1][c-1] != curkey) {
dp[x][y] = (count + 1)%1000000007;
//if you can jump directly to the end then you have an extra path
} else {
dp[x][y] = count;
//otherwise you just have the count
}
}
public static void main(String[] args) throws IOException {
// write your code here
BufferedReader f = new BufferedReader(new FileReader("hopscotch.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("hopscotch.out")));
StringTokenizer st = new StringTokenizer(f.readLine());
int r = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[][] dp = new int[r][c];
int[][] keys = new int[r][c];
for(int i = 0; i<r;i++){
st = new StringTokenizer(f.readLine());
for(int j = 0; j<c;j++){
keys[i][j] = Integer.parseInt(st.nextToken());
}
}
boolean[][] stuff = new boolean[r][c];
dp(0,0,dp,stuff,keys,r,c);
System.out.println(dp[0][0]);
out.println(dp[0][0]);
out.close();
}
}