Skip to content

Commit c4a6cb9

Browse files
Merge pull request #23 from algorithm-cote-study/yeji/leetCode
feat : leetCode Graph활용 문제풀이
2 parents 8ecb9e4 + 3c050c9 commit c4a6cb9

File tree

5 files changed

+93
-119
lines changed

5 files changed

+93
-119
lines changed

.idea/sonarlint/issuestore/index.pb

Lines changed: 0 additions & 119 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/java/sgyj/leetcode/Readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@
2525
## Graph
2626

2727
- [200. Number of Islands](https://leetcode.com/problems/number-of-islands/description/)
28+
- [1091. Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix/)
29+
- [841. Keys and Rooms](https://leetcode.com/problems/keys-and-rooms/description/)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package sgyj.leetcode.yeji.section5;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
6+
// 1091. Shortest Path in Binary Matrix
7+
public class Solution1091 {
8+
private static int[] dx = {-1,1,0,0,1,1,-1,-1};
9+
private static int[] dy = {0,0,-1,1,1,-1,-1,1};
10+
private static boolean[][] visited;
11+
12+
public static int shortestPathBinaryMatrix(int[][] grid) {
13+
int answer = -1;
14+
int n = grid.length;
15+
visited = new boolean[n][n];
16+
17+
Deque<Location> q = new ArrayDeque<>();
18+
if(grid[0][0] != 0) return -1;
19+
visited[0][0] = true;
20+
q.offer( new Location(0,0,1 ) );
21+
while ( !q.isEmpty() ){
22+
int len = q.size();
23+
for(int i=0; i<len; i++){
24+
Location curNode = q.poll();
25+
if(curNode.x == n-1 && curNode.y == n-1) {
26+
answer = curNode.len;
27+
break;
28+
}
29+
for(int d=0; d<dx.length; d++){
30+
int x = curNode.x + dx[d];
31+
int y = curNode.y + dy[d];
32+
if(x>=0 && x<n && y>=0 && y<n && !visited[x][y] && grid[x][y] == 0){
33+
visited[x][y] = true;
34+
q.offer( new Location( x,y , curNode.len+1 ) );
35+
}
36+
}
37+
}
38+
}
39+
return answer;
40+
}
41+
42+
public static void main ( String[] args ) {
43+
int[][] grid = {
44+
// [[0,0,0],[1,1,0],[1,1,0]]
45+
{0,0,0},{1,1,0},{1,1,0}
46+
};
47+
48+
System.out.println(shortestPathBinaryMatrix( grid ));
49+
}
50+
}

src/main/java/sgyj/leetcode/yeji/section5/Solution200.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,16 @@ class Location{
6666
int x;
6767
int y;
6868

69+
int len;
70+
6971
Location(int x, int y){
7072
this.x = x;
7173
this.y = y;
7274
}
75+
76+
Location(int x, int y, int len){
77+
this.x = x;
78+
this.y = y;
79+
this.len = len;
80+
}
7381
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package sgyj.leetcode.yeji.section5;
2+
3+
import java.util.ArrayDeque;
4+
import java.util.Deque;
5+
import java.util.List;
6+
7+
// 841. Keys and Rooms
8+
public class Solution841 {
9+
private static boolean[] visited;
10+
11+
public boolean canVisitAllRooms(List<List<Integer>> rooms) {
12+
int n = rooms.size();
13+
visited = new boolean[n];
14+
Deque<Integer> q = new ArrayDeque<>();
15+
q.offer(0);
16+
visited[0] = true;
17+
while(!q.isEmpty()){
18+
int curNode = q.poll();
19+
for(int i=0; i<rooms.get(curNode).size(); i++){
20+
int target = rooms.get(curNode).get(i);
21+
if(!visited[target]){
22+
q.offer(target);
23+
visited[target] = true;
24+
}
25+
}
26+
}
27+
28+
for(boolean v : visited){
29+
if(!v) return false;
30+
}
31+
return true;
32+
}
33+
}

0 commit comments

Comments
 (0)