-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotMovements.java
More file actions
51 lines (45 loc) · 1.15 KB
/
RobotMovements.java
File metadata and controls
51 lines (45 loc) · 1.15 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
public class RobotMovements {
int m, n;
int[][] grid;
public int computeMovements(int[][] mat, int curX, int curY) {
int l, r, u, d;
if (curX == m - 1 && curY == n - 1) {
return 1;
}
l = 0; r = 0; u = 0; d = 0;
if (curX + 1 < m && mat[curX+1][curY] != 1) {
mat[curX+1][curY] = 1;
d = computeMovements(mat, curX + 1, curY);
mat[curX+1][curY] = 0;
}
if (curY + 1 < n && mat[curX][curY+1] != 1) {
mat[curX][curY+1] = 1;
r = computeMovements(mat, curX, curY + 1);
mat[curX][curY+1] = 0;
}
if (curX - 1 >= 0 && mat[curX-1][curY] != 1) {
mat[curX-1][curY] = 1;
u = computeMovements(mat, curX - 1, curY);
mat[curX-1][curY] = 0;
}
if (curY - 1 >= 0 && mat[curX][curY-1] != 1) {
mat[curX][curY-1] = 1;
l = computeMovements(mat, curX, curY - 1);
mat[curX][curY-1] = 0;
}
return (l+r+u+d);
}
public RobotMovements (int row, int col) {
m = row;
n = col;
grid = new int[m][n];
grid[0][0] = 1;
}
public int[][] getGrid() {
return grid;
}
public static void main(String[] args) {
RobotMovements rm = new RobotMovements(4,4);
System.out.println(rm.computeMovements(rm.getGrid(),0,0));
}
}