Skip to content

Commit 8e29716

Browse files
committed
update 1091 java
1 parent c977904 commit 8e29716

File tree

2 files changed

+175
-40
lines changed

2 files changed

+175
-40
lines changed

leetcode_java/src/main/java/LeetCodeJava/BFS/ShortestPathInBinaryMatrix.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,67 @@ public int shortestPathBinaryMatrix(int[][] grid) {
100100
return -1;
101101
}
102102

103+
// V0-1
104+
// IDEA: BFS (fixed by gpt)
105+
public int shortestPathBinaryMatrix_0_1(int[][] grid) {
106+
int n = grid.length;
107+
108+
// Edge case: Start or end cell is blocked
109+
if (grid[0][0] != 0 || grid[n - 1][n - 1] != 0) {
110+
return -1;
111+
}
112+
113+
// Directions for 8-connected neighbors
114+
int[][] directions = {
115+
{ 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 },
116+
{ -1, -1 }, { -1, 1 }, { 1, 1 }, { 1, -1 }
117+
};
118+
119+
// BFS queue: {x, y, pathLength}
120+
Queue<int[]> queue = new LinkedList<>();
121+
122+
/** NOTE !!! init visisted as boolean array */
123+
boolean[][] visited = new boolean[n][n];
124+
125+
// Initialize BFS
126+
/**
127+
* Queue : {x, y, path_len}
128+
*/
129+
queue.add(new int[] { 0, 0, 1 });
130+
visited[0][0] = true;
131+
132+
// BFS
133+
while (!queue.isEmpty()) {
134+
int[] current = queue.poll();
135+
int x = current[0];
136+
int y = current[1];
137+
int pathLength = current[2];
138+
139+
// Check if we've reached the destination
140+
/** NOTE !!! return res directly if it can reach `final status` */
141+
if (x == n - 1 && y == n - 1) {
142+
return pathLength;
143+
}
144+
145+
// Explore all neighbors
146+
for (int[] dir : directions) {
147+
int newX = x + dir[0];
148+
int newY = y + dir[1];
149+
150+
// Check if the neighbor is valid
151+
if (newX >= 0 && newX < n && newY >= 0 && newY < n
152+
&& grid[newX][newY] == 0 && !visited[newX][newY]) {
153+
queue.add(new int[] { newX, newY, pathLength + 1 });
154+
/** NOTE !!! update seen status */
155+
visited[newX][newY] = true;
156+
}
157+
}
158+
}
159+
160+
// If no path is found
161+
return -1;
162+
}
163+
103164
// V1
104165
// https://leetcode.com/problems/shortest-path-in-binary-matrix/solutions/2043319/why-use-bfs-search-every-possible-path-v-aaov/
105166
// IDEA: BFS

leetcode_java/src/main/java/dev/workspace6.java

Lines changed: 114 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,7 +1920,7 @@ private void buildRelation(List<List<String>> equations, double[] values){
19201920
}
19211921

19221922
// LC 1091
1923-
// 8.06 - 8.30 pm
1923+
// 7.09 am - 7.15 am
19241924
// https://leetcode.com/problems/shortest-path-in-binary-matrix/
19251925
/**
19261926
* Given an n x n binary matrix grid,
@@ -1946,65 +1946,139 @@ private void buildRelation(List<List<String>> equations, double[] values){
19461946
*
19471947
*/
19481948
public int shortestPathBinaryMatrix(int[][] grid) {
1949-
1950-
int l = grid.length;
1951-
int w = grid[0].length;
1952-
19531949
// edge
1954-
if (l == 1 && w == 1){
1955-
return 1;
1950+
if (grid.length == 1 && grid[0].length == 1) {
1951+
if (grid[0][0] == 0) {
1952+
return 1;
1953+
}
1954+
return -1;
19561955
}
19571956

1958-
// bfs
1959-
int res = 0;
1960-
int[][] dirs = new int[][]{ {0,1}, {0,-1}, {1,0}, {-1,0}, {-1,-1}, {-1,1} , {1,1}, {1,-1} };
1957+
//int res = -1;
1958+
1959+
int[][] dirs = new int[][] { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 }, { -1, -1 }, { -1, 1 }, { 1, 1 },
1960+
{ 1, -1 } };
19611961
/**
1962-
* Queue : {x, y, path_len}
1962+
* Queue : {x, y, path_len}
19631963
*/
19641964
Queue<List<Integer>> queue = new LinkedList<>();
1965-
//int[] tmp = new int[]{0};
1966-
//int[][] tmp = new int[][]{ {0}, {0} };
1967-
List<Integer> tmp = new ArrayList<>();
1968-
tmp.add(0);
1969-
tmp.add(0);
1970-
tmp.add(0); // path len
1971-
1972-
Set<List<Integer>> visited = new HashSet<>();
1965+
Set<List<Integer>> seen = new HashSet<>();
1966+
int l = grid.length;
1967+
int w = grid[0].length;
19731968

1974-
//queue
1975-
queue.add(tmp);
1969+
// init
1970+
queue.add(Arrays.asList(0, 0, 1));
1971+
seen.add(Arrays.asList(0, 0));
19761972

1977-
int curLen = 0;
1973+
List<Integer> candidate = new ArrayList<>();
19781974

1979-
while(!queue.isEmpty()){
1980-
List<Integer> cur = queue.poll();
1981-
int x = cur.get(0);
1982-
int y = cur.get(1);
1983-
int dist = cur.get(2);
1984-
for (int[] dir: dirs){
1975+
// bfs
1976+
while (!queue.isEmpty()) {
1977+
List<Integer> tmp = queue.poll();
1978+
int x = tmp.get(0);
1979+
int y = tmp.get(1);
1980+
int pathLen = tmp.get(2);
1981+
// res = pathLen;
1982+
for (int[] dir : dirs) {
19851983
int x_ = x + dir[0];
19861984
int y_ = y + dir[1];
1987-
List<Integer> tmp2 = new ArrayList<>();
1988-
tmp2.add(x_);
1989-
tmp2.add(y_);
19901985

1991-
if (x_ == w-1 && y_ == l-1){
1992-
curLen = dist;
1986+
if (x_ == w - 1 && y_ == l - 1) {
1987+
candidate.add(pathLen+1);
19931988
break;
19941989
}
19951990

1991+
List<Integer> tmp2 = Arrays.asList(x_, y_);
1992+
seen.add(tmp2);
1993+
1994+
System.out.println(">>> x_ = " + x_ + ", y_ = " + y_);
19961995

1997-
if (x_ >= 0 && x_ < w && y_ >= 0 && y_ < l && !visited.contains(tmp2) && grid[y_][x_] == 0){
1998-
dist += 1;
1999-
tmp2.add(2, dist);
2000-
queue.add(tmp2);
2001-
visited.add(tmp2);
1996+
if (x_ >= 0 && x_ < w && y_ >= 0 && y_ < l && !seen.contains(tmp2) && grid[y_][x_] == 0) {
1997+
queue.add(Arrays.asList(x_, y_, pathLen+1));
1998+
}else{
1999+
// undo add
2000+
seen.remove(tmp2);
20022001
}
2002+
20032003
}
20042004
}
20052005

2006+
System.out.println(">>> candidate = " + candidate);
2007+
System.out.println(">>> candidate.isEmpty() = " + candidate.isEmpty());
20062008

2007-
return curLen != 0 ? res : -1;
2008-
}
2009+
if(candidate.isEmpty()){
2010+
return -1;
2011+
}
2012+
2013+
int res = Integer.MAX_VALUE;
2014+
if (candidate.size() > 1) {
2015+
for(int x: candidate){
2016+
res = Math.min(res, x);
2017+
}
2018+
}
2019+
return res;
2020+
}
2021+
2022+
// public int shortestPathBinaryMatrix(int[][] grid) {
2023+
//
2024+
// int l = grid.length;
2025+
// int w = grid[0].length;
2026+
//
2027+
// // edge
2028+
// if (l == 1 && w == 1){
2029+
// return 1;
2030+
// }
2031+
//
2032+
// // bfs
2033+
// int res = 0;
2034+
// int[][] dirs = new int[][]{ {0,1}, {0,-1}, {1,0}, {-1,0}, {-1,-1}, {-1,1} , {1,1}, {1,-1} };
2035+
// /**
2036+
// * Queue : {x, y, path_len}
2037+
// */
2038+
// Queue<List<Integer>> queue = new LinkedList<>();
2039+
// //int[] tmp = new int[]{0};
2040+
// //int[][] tmp = new int[][]{ {0}, {0} };
2041+
// List<Integer> tmp = new ArrayList<>();
2042+
// tmp.add(0);
2043+
// tmp.add(0);
2044+
// tmp.add(0); // path len
2045+
//
2046+
// Set<List<Integer>> visited = new HashSet<>();
2047+
//
2048+
// //queue
2049+
// queue.add(tmp);
2050+
//
2051+
// int curLen = 0;
2052+
//
2053+
// while(!queue.isEmpty()){
2054+
// List<Integer> cur = queue.poll();
2055+
// int x = cur.get(0);
2056+
// int y = cur.get(1);
2057+
// int dist = cur.get(2);
2058+
// for (int[] dir: dirs){
2059+
// int x_ = x + dir[0];
2060+
// int y_ = y + dir[1];
2061+
// List<Integer> tmp2 = new ArrayList<>();
2062+
// tmp2.add(x_);
2063+
// tmp2.add(y_);
2064+
//
2065+
// if (x_ == w-1 && y_ == l-1){
2066+
// curLen = dist;
2067+
// break;
2068+
// }
2069+
//
2070+
//
2071+
// if (x_ >= 0 && x_ < w && y_ >= 0 && y_ < l && !visited.contains(tmp2) && grid[y_][x_] == 0){
2072+
// dist += 1;
2073+
// tmp2.add(2, dist);
2074+
// queue.add(tmp2);
2075+
// visited.add(tmp2);
2076+
// }
2077+
// }
2078+
// }
2079+
//
2080+
//
2081+
// return curLen != 0 ? res : -1;
2082+
// }
20092083

20102084
}

0 commit comments

Comments
 (0)