Skip to content

Commit 2ff17c0

Browse files
add solution for 980
1 parent d8f8c2f commit 2ff17c0

File tree

2 files changed

+75
-10
lines changed

2 files changed

+75
-10
lines changed

src/main/java/com/fishercoder/solutions/_980.java

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,60 @@
3838
* */
3939
public class _980 {
4040
public static class Solution1 {
41+
42+
int[] directions = new int[]{0, 1, 0, -1, 0};
43+
int paths = 0;
44+
4145
public int uniquePathsIII(int[][] grid) {
42-
//TODO: implement it
43-
return -1;
46+
int[] start = findStart(grid);
47+
int m = grid.length;
48+
int n = grid[0].length;
49+
boolean[][] visited = new boolean[m][n];
50+
visited[start[0]][start[1]] = true;
51+
return backtracking(grid, m, n, visited, start);
52+
}
53+
54+
private int backtracking(int[][] grid, int m, int n, boolean[][] visited, int[] start) {
55+
for (int i = 0; i < directions.length - 1; i++) {
56+
int nextX = directions[i] + start[0];
57+
int nextY = directions[i + 1] + start[1];
58+
if (nextX >= 0 && nextX < m && nextY >= 0 && nextY < n && grid[nextX][nextY] != -1 && !visited[nextX][nextY]) {
59+
if (grid[nextX][nextY] == 2) {
60+
if (allZeroesVisited(visited, grid)) {
61+
paths++;
62+
return paths;
63+
} else {
64+
continue;
65+
}
66+
}
67+
visited[nextX][nextY] = true;
68+
backtracking(grid, m, n, visited, new int[]{nextX, nextY});
69+
visited[nextX][nextY] = false;
70+
}
71+
}
72+
return paths;
73+
}
74+
75+
private boolean allZeroesVisited(boolean[][] visited, int[][] grid) {
76+
for (int i = 0; i < grid.length; i++) {
77+
for (int j = 0; j < grid[0].length; j++) {
78+
if (grid[i][j] == 0 && !visited[i][j]) {
79+
return false;
80+
}
81+
}
82+
}
83+
return true;
84+
}
85+
86+
private int[] findStart(int[][] grid) {
87+
for (int i = 0; i < grid.length; i++) {
88+
for (int j = 0; j < grid[0].length; j++) {
89+
if (grid[i][j] == 1) {
90+
return new int[]{i, j};
91+
}
92+
}
93+
}
94+
return null;
4495
}
4596
}
4697
}
Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._980;
4-
import org.junit.BeforeClass;
5-
import org.junit.Ignore;
64
import org.junit.Test;
75

86
import static org.junit.Assert.assertEquals;
97

10-
@Ignore
118
public class _980Test {
129
private static _980.Solution1 solution1;
1310
private static int[][] grid;
1411

15-
@BeforeClass
16-
public static void setup() {
17-
solution1 = new _980.Solution1();
18-
}
19-
2012
@Test
2113
public void test1() {
14+
solution1 = new _980.Solution1();
2215
grid = new int[][]{
2316
{1, 0, 0, 0},
2417
{0, 0, 0, 0},
@@ -27,4 +20,25 @@ public void test1() {
2720
assertEquals(2, solution1.uniquePathsIII(grid));
2821
}
2922

23+
@Test
24+
public void test2() {
25+
solution1 = new _980.Solution1();
26+
grid = new int[][]{
27+
{1, 0, 0, 0},
28+
{0, 0, 0, 0},
29+
{0, 0, 0, 2},
30+
};
31+
assertEquals(4, solution1.uniquePathsIII(grid));
32+
}
33+
34+
@Test
35+
public void test3() {
36+
solution1 = new _980.Solution1();
37+
grid = new int[][]{
38+
{0, 1},
39+
{2, 0},
40+
};
41+
assertEquals(0, solution1.uniquePathsIII(grid));
42+
}
43+
3044
}

0 commit comments

Comments
 (0)