Skip to content

Commit 9add9e9

Browse files
committedApr 25, 2024
Merge branch 'week3'
2 parents cb4d4a0 + fb74091 commit 9add9e9

6 files changed

+295
-0
lines changed
 

‎week3/BOJ_11403(경로 찾기).java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static List<List<Integer>> list;
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
int N = Integer.parseInt(br.readLine());
11+
list = new ArrayList<>();
12+
for(int i=0; i<N; i++) {
13+
list.add(new ArrayList<>());
14+
}
15+
for(int i=0; i<N; i++) {
16+
StringTokenizer st = new StringTokenizer(br.readLine());
17+
for(int j=0; j<N; j++) {
18+
int edge = Integer.parseInt(st.nextToken());
19+
if(edge == 1) {
20+
list.get(i).add(j);
21+
}
22+
}
23+
}
24+
StringBuffer sb = new StringBuffer();
25+
for(int i=0; i<N; i++) {
26+
int[] visited = new int[N];
27+
dfs(i, visited);
28+
for(int j=0; j<N; j++) {
29+
sb.append(visited[j]).append(' ');
30+
}
31+
sb.append('\n');
32+
}
33+
System.out.println(sb);
34+
}
35+
36+
public static void dfs(int n, int[] visited) {
37+
List<Integer> edge = list.get(n);
38+
for(int i=0; i<edge.size(); i++) {
39+
if(visited[edge.get(i)] != 1) {
40+
visited[edge.get(i)] = 1;
41+
dfs(edge.get(i), visited);
42+
}
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static List<List<Integer>> graph;
7+
public static int[] visited;
8+
9+
public static void main(String[] args) throws IOException {
10+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
int N = Integer.parseInt(br.readLine());
12+
graph = new ArrayList<>();
13+
visited = new int[N+1];
14+
for(int i=0; i<=N; i++) {
15+
graph.add(new ArrayList<>());
16+
}
17+
for(int i=0; i<N-1; i++) {
18+
StringTokenizer st = new StringTokenizer(br.readLine());
19+
int v1 = Integer.parseInt(st.nextToken());
20+
int v2 = Integer.parseInt(st.nextToken());
21+
graph.get(v1).add(v2);
22+
graph.get(v2).add(v1);
23+
}
24+
25+
dfs(1);
26+
27+
StringBuffer sb = new StringBuffer();
28+
for(int i=2; i<=N; i++) {
29+
sb.append(visited[i]).append('\n');
30+
}
31+
System.out.print(sb);
32+
}
33+
34+
public static void dfs(int n) {
35+
List<Integer> list = graph.get(n);
36+
for(int i=0; i<list.size(); i++) {
37+
int v = list.get(i);
38+
if(visited[v] == 0) {
39+
visited[v] = n;
40+
dfs(v);
41+
}
42+
}
43+
}
44+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
int N = Integer.parseInt(br.readLine());
9+
StringTokenizer st = new StringTokenizer(br.readLine());
10+
int r1 = Integer.parseInt(st.nextToken());
11+
int c1 = Integer.parseInt(st.nextToken());
12+
int r2 = Integer.parseInt(st.nextToken());
13+
int c2 = Integer.parseInt(st.nextToken());
14+
15+
int[][] visited = new int[N+1][N+1];
16+
int[] dh = {-2, -2, 0, 0, 2, 2};
17+
int[] dw = {-1, 1, -2, 2, -1, 1};
18+
Queue<int[]> q = new LinkedList<>();
19+
q.offer(new int[]{r1, c1});
20+
while(!q.isEmpty()) {
21+
int h = q.peek()[0];
22+
int w = q.peek()[1];
23+
q.poll();
24+
if(h == r2 && w == c2) {
25+
System.out.println(visited[h][w]);
26+
return;
27+
}
28+
29+
for(int i=0; i<6; i++) {
30+
int nextH = h + dh[i];
31+
int nextW = w + dw[i];
32+
if(nextH >= 0 && nextH < N && nextW >= 0 && nextW < N) {
33+
if(visited[nextH][nextW] == 0) {
34+
visited[nextH][nextW] = visited[h][w] + 1;
35+
q.offer(new int[]{nextH, nextW});
36+
}
37+
}
38+
}
39+
}
40+
System.out.println("-1");
41+
}
42+
}

‎week3/BOJ_17086(아기 상어 2).java

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package week3;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
public static int N, M;
9+
public static int[][] shark;
10+
11+
public static void main(String[] args) throws IOException {
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
String[] input = br.readLine().split(" ");
14+
N = Integer.parseInt(input[0]);
15+
M = Integer.parseInt(input[1]);
16+
shark = new int[N][M];
17+
for(int i=0; i<N; i++) {
18+
StringTokenizer st = new StringTokenizer(br.readLine());
19+
for(int j=0; j<M; j++) {
20+
shark[i][j] = Integer.parseInt(st.nextToken());
21+
}
22+
}
23+
int max = Integer.MIN_VALUE;
24+
for(int i=0; i<N; i++) {
25+
for(int j=0; j<M; j++) {
26+
if(shark[i][j] == 0) {
27+
max = Math.max(max, getSafeDistance(i, j));
28+
}
29+
}
30+
}
31+
System.out.println(max);
32+
}
33+
34+
public static int getSafeDistance(int h, int w) {
35+
int[] dh = {-1, -1, -1, 0, 0, 1, 1, 1};
36+
int[] dw = {-1, 0, 1, -1, 1, -1, 0, 1};
37+
38+
int[][] visited = new int[N][M];
39+
int min = Integer.MAX_VALUE;
40+
Queue<int[]> q = new LinkedList<>();
41+
q.offer(new int[]{h, w});
42+
visited[h][w] = 1;
43+
while(!q.isEmpty()) {
44+
h = q.peek()[0];
45+
w = q.peek()[1];
46+
q.poll();
47+
48+
for(int i=0; i<8; i++) {
49+
int nextH = h + dh[i];
50+
int nextW = w + dw[i];
51+
if(nextH >=0 && nextH < N && nextW >=0 && nextW < M) {
52+
if(visited[nextH][nextW] == 0) {
53+
if(shark[nextH][nextW] == 0) {
54+
visited[nextH][nextW] = visited[h][w] + 1;
55+
q.offer(new int[]{nextH, nextW});
56+
}else {
57+
min = Math.min(min, visited[h][w]);
58+
}
59+
}
60+
}
61+
}
62+
}
63+
return min;
64+
}
65+
}

‎week3/BOJ_2468(안전 영역).java

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static int N;
7+
public static int[][] map;
8+
public static int[][] visited;
9+
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
N = Integer.parseInt(br.readLine());
13+
map = new int[N+1][N+1];
14+
int max = Integer.MIN_VALUE;
15+
for(int i=0; i<N; i++) {
16+
StringTokenizer st = new StringTokenizer(br.readLine());
17+
for(int j=0; j<N; j++) {
18+
map[i][j] = Integer.parseInt(st.nextToken());
19+
max = Math.max(max, map[i][j]);
20+
}
21+
}
22+
int answer = 1;
23+
for(int k=1; k<=max; k++) {
24+
visited = new int[N+1][N+1];
25+
int count = 0;
26+
for(int i=0; i<N; i++) {
27+
for(int j=0; j<N; j++) {
28+
if(visited[i][j] == 0 && map[i][j] > k) {
29+
count++;
30+
bfs(i, j, k, count);
31+
}
32+
}
33+
}
34+
answer = Math.max(answer, count);
35+
}
36+
System.out.println(answer);
37+
}
38+
39+
public static void bfs(int h, int w, int depth, int count) {
40+
int[] dh = {-1, 0, 1, 0};
41+
int[] dw = {0, 1, 0, -1};
42+
Queue<int[]> q = new LinkedList<>();
43+
q.offer(new int[]{h, w});
44+
visited[h][w] = 1;
45+
while(!q.isEmpty()) {
46+
h = q.peek()[0];
47+
w = q.peek()[1];
48+
q.poll();
49+
50+
for(int i=0; i<4; i++) {
51+
int nextH = h + dh[i];
52+
int nextW = w + dw[i];
53+
if(nextH >= 0 && nextH < N && nextW >= 0 && nextW < N) {
54+
if(visited[nextH][nextW] == 0 && map[nextH][nextW] > depth) {
55+
visited[nextH][nextW] = count;
56+
q.offer(new int[]{nextH, nextW});
57+
}
58+
}
59+
}
60+
}
61+
}
62+
}

‎week3/BOJ_5014(스타트링크).java

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
StringTokenizer st = new StringTokenizer(br.readLine());
9+
int topFloor = Integer.parseInt(st.nextToken());
10+
int currentFloor = Integer.parseInt(st.nextToken());
11+
int targetFloor = Integer.parseInt(st.nextToken());
12+
int up = Integer.parseInt(st.nextToken());
13+
int down = Integer.parseInt(st.nextToken());
14+
15+
int[] visited = new int[topFloor+1];
16+
Queue<Integer> q = new LinkedList<>();
17+
q.offer(currentFloor);
18+
visited[currentFloor] = 1;
19+
while(!q.isEmpty()) {
20+
int f = q.poll();
21+
if(f == targetFloor) {
22+
System.out.println(visited[targetFloor]-1);
23+
return;
24+
}
25+
if(f+up <= topFloor && visited[f+up] == 0) {
26+
visited[f+up] = visited[f]+1;
27+
q.offer(f+up);
28+
}
29+
30+
if(f-down >= 1 && visited[f-down] == 0) {
31+
visited[f-down] = visited[f]+1;
32+
q.offer(f-down);
33+
}
34+
}
35+
System.out.println("use the stairs");
36+
}
37+
}

0 commit comments

Comments
 (0)
Please sign in to comment.