Skip to content

Commit 9296726

Browse files
committed
Merge branch 'week9'
2 parents 6006987 + 75e497a commit 9296726

4 files changed

+158
-0
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
3+
private boolean[] visited;
4+
5+
public int solution(int n, int[][] computers) {
6+
int count = 0;
7+
visited = new boolean[n];
8+
for(int i=0; i<n; i++) {
9+
if(!visited[i]) {
10+
count++;
11+
dfs(computers, i);
12+
}
13+
}
14+
return count;
15+
}
16+
17+
public void dfs(int[][] computers, int v) {
18+
int len = computers.length;
19+
for(int i=0; i<len; i++) {
20+
if(!visited[i] && computers[v][i] == 1) {
21+
visited[i] = true;
22+
dfs(computers, i);
23+
}
24+
}
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
3+
private int count = 0;
4+
5+
public int solution(int[] numbers, int target) {
6+
dfs(numbers, target, 0, 0);
7+
return count;
8+
}
9+
10+
public void dfs(int[] numbers, int target, int index, int n) {
11+
if(index == numbers.length) {
12+
if(n == target) {
13+
count++;
14+
}
15+
return;
16+
}
17+
18+
dfs(numbers, target, index+1, n + numbers[index]);
19+
dfs(numbers, target, index+1, n - numbers[index]);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
5+
private int[][] board;
6+
7+
public int solution(int[][] rectangle, int characterX, int characterY, int itemX, int itemY) {
8+
int len = rectangle.length;
9+
board = new int[101][101];
10+
11+
// 좌표를 두 배로 확대해서 도형의 테두리 공간을 만든다
12+
for(int i=0; i<len; i++) {
13+
int x1 = rectangle[i][0] * 2;
14+
int y1 = rectangle[i][1] * 2;
15+
int x2 = rectangle[i][2] * 2;
16+
int y2 = rectangle[i][3] * 2;
17+
18+
// 좌표*2 도형 내부를 1로 채우기
19+
for(int j=y1; j<=y2; j++) {
20+
for(int k=x1; k<=x2; k++) {
21+
board[j][k] = 1;
22+
}
23+
}
24+
}
25+
26+
for(int i=0; i<len; i++) {
27+
int x1 = rectangle[i][0] * 2;
28+
int y1 = rectangle[i][1] * 2;
29+
int x2 = rectangle[i][2] * 2;
30+
int y2 = rectangle[i][3] * 2;
31+
32+
// 도형의 테두리만 남기고 나머지를 0으로 채우기
33+
for(int j=y1+1; j<=y2-1; j++) {
34+
for(int k=x1+1; k<=x2-1; k++) {
35+
board[j][k] = 0;
36+
}
37+
}
38+
}
39+
int result = bfs(rectangle, characterX*2, characterY*2, itemX*2, itemY*2);
40+
return result/2;
41+
}
42+
43+
public int bfs(int[][] rectangle, int characterW, int characterH, int itemW, int itemH) {
44+
int[] dh = {0, 1, 0, -1};
45+
int[] dw = {-1, 0, 1, 0};
46+
int[][] visited = new int[101][101];
47+
48+
Queue<int[]> q = new LinkedList<>();
49+
q.offer(new int[]{characterH, characterW});
50+
while(!q.isEmpty()) {
51+
int h = q.peek()[0];
52+
int w = q.peek()[1];
53+
q.poll();
54+
55+
if(h == itemH && w == itemW) {
56+
return visited[h][w];
57+
}
58+
59+
for(int i=0; i<4; i++) {
60+
int nextH = h + dh[i];
61+
int nextW = w + dw[i];
62+
if(nextH >= 0 && nextH <= 100 && nextW >= 0 && nextW <= 100) {
63+
if(visited[nextH][nextW] == 0 && board[nextH][nextW] == 1) {
64+
visited[nextH][nextW] = visited[h][w]+1;
65+
q.offer(new int[]{nextH, nextW});
66+
}
67+
}
68+
}
69+
}
70+
return 0;
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class Solution {
2+
3+
private boolean[] visited;
4+
private int answer = 0;
5+
private int[] info;
6+
private int[][] edges;
7+
8+
public int solution(int[] _info, int[][] _edges) {
9+
answer = 0;
10+
info = _info;
11+
edges = _edges;
12+
visited = new boolean[info.length+1];
13+
visited[0] = true;
14+
dfs(1, 0);
15+
return answer;
16+
}
17+
18+
public void dfs(int sheep, int wolf) {
19+
if(wolf < sheep) {
20+
answer = Math.max(answer, sheep);
21+
}else {
22+
return;
23+
}
24+
25+
for(int i=0; i<edges.length; i++) {
26+
int parent = edges[i][0];
27+
int child = edges[i][1];
28+
if(visited[parent] && !visited[child]) {
29+
visited[child] = true;
30+
if(info[child] == 0) {
31+
dfs(sheep+1, wolf);
32+
}else {
33+
dfs(sheep, wolf+1);
34+
}
35+
visited[child] = false;
36+
}
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)