
https://leetcode.com/problems/shortest-path-in-binary-matrix/description/
n x n ์ด์ง ํ๋ ฌ grid๊ฐ ์ฃผ์ด์ก์ ๋, ํ๋ ฌ์์ ๊ฐ์ฅ ์งง์ clear path๋ฅผ ๋ฆฌํดํ๋ผ.(๋จ, clear path๊ฐ ์์ผ๋ฉด -1์ ๋ฆฌํดํ๋ผ)
clear path: (0,0) -> (n-1, n-1)๋ก ๊ฐ๋ path๋ฅผ ๋งํจ:
1. path์ ๋ชจ๋ ๋ฐฉ๋ฌธํ ์
๋ค์ 0์ด๋ค.
2. path์ ์ธ์ ํ ์
๋ค์ 8-directionally๋ก ์ฐ๊ฒฐ๋์ด ์๋ค.(๊ทธ๊ฒ๋ค์ ๋ชจ๋ ๋ค๋ฅด๊ณ , ๊ทธ๊ฒ๋ค์ ๊ฐ์ ๋๋ ๊ตฌ์์ ๊ณต์ ํ๋ค.)
3. clear path์ ๊ธธ์ด๋ ๋ฐฉ๋ฌธํ ์
๋ค์ ์์ด๋ค.
๐ง์ ์ฝ ์กฐ๊ฑด
1. m = row, n = col
2. 1 <= m, n <= 100
=> m*n = 10000 < 10^8 ์ด๋ฏ๋ก brute-force ๊ฐ๋ฅ
3. grid[i][j]๋ 0 ๋๋ 1์ด๋ค.
public class ShortestPathSolution {
public int shortestPathBinaryMatrix(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
final int[] dr = {-1, 1, 0, 0, -1, 1, 1, -1};
final int[] dc = {0, 0, -1, 1, -1, 1, -1, 1};
boolean[][] visited = new boolean[m][n];
int count = 0;
// ์์ ๋๋ ์ข
์ ์ด 1์ด๋ฉด clear path๊ฐ ์๋๋ฏ๋ก -1์ ๋ฆฌํด
if (grid[0][0] == 1 || grid[m - 1][n - 1] == 1) {
return -1;
}
// ์์ ํ์
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 0 && !visited[i][j]) {
// bfs ํ์ ์์
visited[i][j] = true;
Queue<Point> q = new LinkedList<>();
q.offer(new Point(i, j, 1));
while (!q.isEmpty()) {
Point cur = q.poll();
int startR = cur.r;
int startC = cur.c;
int curCount = cur.count;
// ์
๋ฐ์ดํธ
count = Math.max(count, curCount);
// ํ์ฌ ์ขํ๊ฐ ์ข
์ ์ด๋ฉด count๋ฅผ ๋ฆฌํด
if ((startR == m - 1) && (startC == n - 1)) {
return count;
}
for (int k = 0; k < 8; k++) {
int nextR = startR + dr[k];
int nextC = startC + dc[k];
if ((nextR >= 0 && nextR < m) && (nextC >= 0 && nextC < n) && grid[nextR][nextC] == 0 && !visited[nextR][nextC]) {
visited[nextR][nextC] = true;
q.offer(new Point(nextR, nextC, curCount + 1));
}
}
}
return -1;
}
}
}
return count;
}
private static class Point {
int r;
int c;
int count;
public Point(int r, int c, int count) {
this.r = r;
this.c = c;
this.count = count;
}
}
}
๐์คํ ๋ฆฌ๋ ๋ค์๊ณผ ๊ฐ๋ค:
1. ์์ ๋๋ ์ข
์ ์ด 1์ด๋ฉด clear path๊ฐ ์๋๋ฏ๋ก -1์ ๋ฆฌํด
2. ์์ ํ์์ ํ๋๋ฐ ํ์ฌ ์ขํ๊ฐ (0,0)์ด๋ฉด bfs ํ์ ์์
2-1) ์
์ ๋ฐฉ๋ฌธํ ๋๋ง๋ค count 1์ฉ ์ฆ๊ฐ์ํจ๋ค.
๐ข์ด๋ ์ฃผ์ํ ์ ์ด 8-directionally๋ก ํ์ํ ๋ (nextR, nextC)์ sibling Node์ฒ๋ผ ์ทจ๊ธ์ ํด์ ๋ฐฉ๋ฌธํ๋ ค๊ณ ํ๋ ์
์ curCount + 1๋ก ๋ฐ๋ก๋ฐ๋ก ์ฒ๋ฆฌํด์ค์ผ ํ๋ค.
2-2) ํ์ฌ ์ขํ๊ฐ ์ข
์ ์ด๋ฉด ์
๋ฐ์ดํธ์ํจ count๋ฅผ ๋ฆฌํด
๐คclear path์ฒ๋ผ ๊ฒฝ๋ก๊ฐ ์ ํด์ง grid๋ ์์ ๊ฐ์ด ์์ ํ์์ ํ ํ์๊ฐ ์๊ณ grid[0][0]์ผ ๋ bfs ํ์์ ๋ฐ๋ก ์์ํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
public class ShortestPathSolution {
public int shortestPathBinaryMatrix(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
final int[] dr = {-1, 1, 0, 0, -1, 1, 1, -1};
final int[] dc = {0, 0, -1, 1, -1, 1, -1, 1};
boolean[][] visited = new boolean[m][n];
// ์์ ๋๋ ์ข
์ ์ด 1์ด๋ฉด clear path๊ฐ ์๋๋ฏ๋ก -1์ ๋ฆฌํด
if (grid[0][0] == 1 || grid[m - 1][n - 1] == 1) {
return -1;
}
// (0,0)์ด 0์ด๋ฉด bfs ํ์ ์์
if (grid[0][0] == 0) {
// bfs ํ์ ์์
visited[0][0] = true;
Queue<Point> q = new LinkedList<>();
q.offer(new Point(0, 0, 1));
while (!q.isEmpty()) {
Point cur = q.poll();
int startR = cur.r;
int startC = cur.c;
int curCount = cur.count;
// ํ์ฌ ์ขํ๊ฐ ์ข
์ ์ด๋ฉด count๋ฅผ ๋ฆฌํด
if ((startR == m - 1) && (startC == n - 1)) {
return curCount;
}
for (int k = 0; k < 8; k++) {
int nextR = startR + dr[k];
int nextC = startC + dc[k];
if ((nextR >= 0 && nextR < m) && (nextC >= 0 && nextC < n) && grid[nextR][nextC] == 0 && !visited[nextR][nextC]) {
visited[nextR][nextC] = true;
q.offer(new Point(nextR, nextC, curCount + 1));
}
}
}
}
return -1;
}
private static class Point {
int r;
int c;
int count;
public Point(int r, int c, int count) {
this.r = r;
this.c = c;
this.count = count;
}
}
}
Math.max(count, curCount)๋ก ์
๋ฐ์ดํธ์ํฌ ํ์๊ฐ ์๋ค.