From e525822233f32343401051f8951b962873d074ce Mon Sep 17 00:00:00 2001 From: yeheun Date: Sat, 1 Mar 2025 15:56:18 +0900 Subject: [PATCH 01/12] =?UTF-8?q?=ED=8F=B4=EB=8D=94=20=EC=97=85=EB=A1=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05_shortest_path | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 05_shortest_path diff --git a/05_shortest_path b/05_shortest_path deleted file mode 100644 index 55694a5..0000000 --- a/05_shortest_path +++ /dev/null @@ -1,20 +0,0 @@ -# 최단 경로 - -### 사이트 링크 - -다익스트라 & 플로이드 문제집, solved.ac에서 선택했습니다. - -### 문제 - -- [지름길 - 실버 1](https://www.acmicpc.net/problem/1446) -- [특정 거리의 도시 찾기 - 실버 2](https://www.acmicpc.net/problem/18352) -- [숨바꼭질 3 - 골드 5](https://www.acmicpc.net/problem/13549) -- [녹색 옷 입은 애가 젤다지? - 골드 4](https://www.acmicpc.net/problem/4485) -- [서강그라운드 - 골드 4](https://www.acmicpc.net/problem/14938) -- [택배 - 골드 3](https://www.acmicpc.net/problem/1719) -- [파티 - 골드 3](https://www.acmicpc.net/problem/1238) - - - -### 안내 -위 7문제 중 난이도에 맞춰 5문제만 푸시면 됩니다! From 42f20ba0d7026a3c6c6c6b99dd1c30e63850fd29 Mon Sep 17 00:00:00 2001 From: yeheun Date: Sat, 1 Mar 2025 15:56:24 +0900 Subject: [PATCH 02/12] =?UTF-8?q?=EC=A7=80=EB=A6=84=EA=B8=B8=5F1446.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05_shortest_path/kim/1446.java | 56 ++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 05_shortest_path/kim/1446.java diff --git a/05_shortest_path/kim/1446.java b/05_shortest_path/kim/1446.java new file mode 100644 index 0000000..5e43a28 --- /dev/null +++ b/05_shortest_path/kim/1446.java @@ -0,0 +1,56 @@ +import java.io.*; +import java.util.*; + + +public class Main { + static class Node{ + int s; + int e; + int w; + Node(int s, int e, int w) { + this.s = s; + this.e = e; + this.w = w; + } + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int D = Integer.parseInt(st.nextToken()); + + int[] dp = new int[10_001]; + Arrays.fill(dp, Integer.MAX_VALUE); + dp[0] = 0; + + Set set = new HashSet<>(); + List list = new ArrayList<>(); + + for(int n = 0; n < N; n++) { + st = new StringTokenizer(br.readLine()); + + int s = Integer.parseInt(st.nextToken()); + int e = Integer.parseInt(st.nextToken()); + int w = Integer.parseInt(st.nextToken()); + + if (e > D) continue; + + list.add(new Node(s, e, w)); + + set.add(s); + set.add(e); + } + + for(int i = 0; i < 10_000; i++) { + dp[i + 1] = Math.min(dp[i+1], dp[i] + 1); + + for(Node now : list) { + if(now.s == i) + dp[now.e] = Math.min(dp[i] + now.w, dp[now.e]); + } + } + + System.out.println(dp[D]); + } +} From 91a5a6986c362b77a0659dd7128b585ede2116ce Mon Sep 17 00:00:00 2001 From: yeheun Date: Sat, 1 Mar 2025 15:56:46 +0900 Subject: [PATCH 03/12] =?UTF-8?q?=ED=8A=B9=EC=A0=95=20=EA=B1=B0=EB=A6=AC?= =?UTF-8?q?=EC=9D=98=20=EB=8F=84=EC=8B=9C=20=EC=B0=BE=EA=B8=B0=5F18352.jav?= =?UTF-8?q?a?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05_shortest_path/kim/18352.java | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 05_shortest_path/kim/18352.java diff --git a/05_shortest_path/kim/18352.java b/05_shortest_path/kim/18352.java new file mode 100644 index 0000000..245a979 --- /dev/null +++ b/05_shortest_path/kim/18352.java @@ -0,0 +1,56 @@ +import java.io.*; +import java.util.*; + + +public class Main { + public static void main(String[] args) throws Exception { + + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + int N = Integer.parseInt(st.nextToken()); // 도시 개수 + int M = Integer.parseInt(st.nextToken()); // 도로 개수 + int K = Integer.parseInt(st.nextToken()); // 거리 정보 + int X = Integer.parseInt(st.nextToken()); // 출발 도시 + + Map> roads = new HashMap<>(); + + for(int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + int s = Integer.parseInt(st.nextToken()); + int e = Integer.parseInt(st.nextToken()); + + List buf = roads.getOrDefault(s, new ArrayList<>()); + buf.add(e); + roads.put(s, buf); + } + + int[] minDist = new int[N+1]; + boolean[] visited = new boolean[N+1]; + + Queue queue = new LinkedList<>(); + queue.add(X); + visited[X] = true; + while(!queue.isEmpty()) { + int now = queue.poll(); + for(int next : roads.getOrDefault(now, new ArrayList<>())) { + if (!visited[next]) { + minDist[next] = minDist[now] + 1; + queue.add(next); + visited[next] = true; + } + } + } + + StringBuilder sb = new StringBuilder(); + boolean isAns = false; + for(int i = 1; i <= N; i++) { + if(minDist[i] == K) { + sb.append(i).append("\n"); + isAns = true; + } + } + + System.out.println(isAns ? sb : -1); + } +} \ No newline at end of file From 32e9b18cfce18f17e209c8e4296a250506e72579 Mon Sep 17 00:00:00 2001 From: yeheun Date: Sat, 1 Mar 2025 15:57:06 +0900 Subject: [PATCH 04/12] =?UTF-8?q?=EC=88=A8=EB=B0=94=EA=BC=AD=EC=A7=883=5F1?= =?UTF-8?q?3549.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05_shortest_path/kim/13549.java | 61 +++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 05_shortest_path/kim/13549.java diff --git a/05_shortest_path/kim/13549.java b/05_shortest_path/kim/13549.java new file mode 100644 index 0000000..52a0f74 --- /dev/null +++ b/05_shortest_path/kim/13549.java @@ -0,0 +1,61 @@ +import java.io.*; +import java.util.*; + +public class Main { + static class Node { + int time; + int now; + + Node(int time, int now) { + this.time = time; + this.now = now; + } + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + int N = Integer.parseInt(st.nextToken()); + int K = Integer.parseInt(st.nextToken()); + + if (N >= K) { + System.out.println(N - K); + return; + } + + int[] go = new int[140_001]; + for (int i = 0; i < go.length; i++) { + go[i] = Integer.MAX_VALUE; + } + + PriorityQueue queue = new PriorityQueue<>((s, e) -> Integer.compare(s.time, e.time)); + queue.add(new Node(0, N)); + + while (!queue.isEmpty()) { + Node now = queue.poll(); + int nnode = now.now; + if (nnode == K) { + System.out.println(now.time); + return; + } + + int[] move = { nnode + 1, nnode - 1 }; + for (int next : move) { + if (next < 0 || next > 140_000) continue; + if (go[next] > now.time + 1 && now.time + 1 <= (K-N)) { + queue.add(new Node(now.time + 1, next)); + go[next] = now.time + 1; + } + } + + if (nnode * 2 <= 0 || nnode * 2 > 140_000) continue; + else { + if (go[nnode * 2] > now.time && now.time <= (K-N)) { + queue.add(new Node(now.time, nnode * 2)); + go[nnode * 2] = now.time; + } + } + } + } +} From e2b5d0f84159dc417a15f1cf379c8ac0da1281e3 Mon Sep 17 00:00:00 2001 From: yeheun Date: Sat, 1 Mar 2025 15:57:26 +0900 Subject: [PATCH 05/12] =?UTF-8?q?=EB=85=B9=EC=83=89=20=EC=98=B7=20?= =?UTF-8?q?=EC=9E=85=EC=9D=80=20=EC=95=A0=EA=B0=80=20=EC=A0=A4=EB=8B=A4?= =?UTF-8?q?=EC=A7=80=3F=5F4485.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05_shortest_path/kim/4485.java | 58 ++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 05_shortest_path/kim/4485.java diff --git a/05_shortest_path/kim/4485.java b/05_shortest_path/kim/4485.java new file mode 100644 index 0000000..21ce30a --- /dev/null +++ b/05_shortest_path/kim/4485.java @@ -0,0 +1,58 @@ +import java.io.*; +import java.util.*; + + +public class Main { + + static int[][] move = {{1,0}, {0,1}, {-1, 0}, {0, -1}}; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringBuilder sb = new StringBuilder(); + + int test = 1; + + while(true) { + int N = Integer.parseInt(br.readLine()); + if (N == 0) break; + + int[][] map = new int[N][N]; + int[][] result = new int[N][N]; + boolean[][] visited = new boolean[N][N]; + + for(int i = 0; i < N; i++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + for(int j = 0; j queue = new LinkedList<>(); + queue.add(new int[] {0, 0}); + visited[0][0] = true; + result[0][0] = map[0][0]; + + while(!queue.isEmpty()) { + int[] now = queue.poll(); + + for(int[] mv : move) { + int nextn = now[0] + mv[0]; + int nextm = now[1] + mv[1]; + + if(nextn < 0 || nextm < 0 || nextn >= N || nextm >= N) continue; + if(visited[nextn][nextm] && result[nextn][nextm] <= result[now[0]][now[1]] + map[nextn][nextm]) continue; + + result[nextn][nextm] = result[now[0]][now[1]] + map[nextn][nextm]; + visited[nextn][nextm] = true; + queue.add(new int[] {nextn, nextm}); + } + } + + sb.append("Problem ").append(test++).append(": ").append(result[N-1][N-1]).append("\n"); + } + + System.out.println(sb); + } +} From 584087a6dc41df462d296eebd12ffb864fd68377 Mon Sep 17 00:00:00 2001 From: yeheun Date: Sat, 1 Mar 2025 15:57:45 +0900 Subject: [PATCH 06/12] =?UTF-8?q?=EC=84=9C=EA=B0=95=EA=B7=B8=EB=9D=BC?= =?UTF-8?q?=EC=9A=B4=EB=93=9C=5F14938.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05_shortest_path/kim/14938.java | 68 +++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 05_shortest_path/kim/14938.java diff --git a/05_shortest_path/kim/14938.java b/05_shortest_path/kim/14938.java new file mode 100644 index 0000000..452bddd --- /dev/null +++ b/05_shortest_path/kim/14938.java @@ -0,0 +1,68 @@ +import java.io.*; +import java.util.*; + + +public class Main { + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringBuilder sb = new StringBuilder(); + + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + int R = Integer.parseInt(st.nextToken()); + + int[] items = new int[N]; + boolean[] visited = new boolean[N]; + int[][] map = new int[N+1][N+1]; + + // 아이템 초기화 + st = new StringTokenizer(br.readLine()); + for(int n = 0; n < N; n++) + items[n] = Integer.parseInt(st.nextToken()); + + // 맵 초기화 + for(int i = 1; i <= N; i++) { + for(int j = 1; j <= N; j++) { + if(i == j) continue; + map[i][j] = Integer.MAX_VALUE; + } + } + + for(int r = 0; r < R; r++) { + st = new StringTokenizer(br.readLine()); + int s = Integer.parseInt(st.nextToken()); + int e = Integer.parseInt(st.nextToken()); + int w = Integer.parseInt(st.nextToken()); + if (w > M) continue; + map[s][e] = w; + map[e][s] = w; + } + + for(int m = 1; m <= N; m++) { + for(int s = 1; s <= N; s++) { + for(int e =1; e <= N; e++) { + if (map[s][m] == Integer.MAX_VALUE || map[m][e] == Integer.MAX_VALUE) continue; + if (map[s][m] + map[m][e] > M) continue; + + map[s][e] = Math.min(map[s][e], map[s][m] + map[m][e]); + } + } + } + + int max = 0; + for(int i = 1; i <= N; i++) { + int buf = 0; + Set set = new HashSet<>(); + + for(int j = 1; j <= N; j++) { + if (map[i][j] != Integer.MAX_VALUE) set.add(j); + } + for(int s : set) buf += items[s-1]; + max = Math.max(max, buf); + } + + System.out.println(max); + } +} From 7f6a54fbcb5d84877b44546fb45f6c2511fcdd36 Mon Sep 17 00:00:00 2001 From: yeheun Date: Sat, 1 Mar 2025 15:58:05 +0900 Subject: [PATCH 07/12] =?UTF-8?q?=ED=83=9D=EB=B0=B0=5F1719.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 05_shortest_path/kim/1719.java | 85 ++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 05_shortest_path/kim/1719.java diff --git a/05_shortest_path/kim/1719.java b/05_shortest_path/kim/1719.java new file mode 100644 index 0000000..67fed39 --- /dev/null +++ b/05_shortest_path/kim/1719.java @@ -0,0 +1,85 @@ +import java.io.*; +import java.util.*; + + +public class Main { + static int N; + static HashMap> routes; + static StringBuilder sb = new StringBuilder(); + + static class Node { + int node; + int dist; + int route; + + Node(int node, int dist, int route) { + this.node = node; + this.dist = dist; + this.route = route; + } + + @Override + public String toString() { + return "{node} " + this.node + " " + this.dist + " " + this.route; + } + } + + public static void dijkstra(int startNode) { + PriorityQueue queue = new PriorityQueue<>((s, e) -> s.dist- e.dist); + + int[] path = new int[N+1]; + path[startNode] = startNode; + for(Node n : routes.getOrDefault(startNode, new ArrayList<>())) { + queue.add(new Node(n.node, n.dist, n.route)); + } + + while (!queue.isEmpty()) { + Node now = queue.poll(); + if (path[now.node] != 0) continue; + path[now.node] = now.route; + for(Node n : routes.getOrDefault(now.node, new ArrayList<>())) { + if (path[n.node] == 0) { + queue.add(new Node(n.node, now.dist + n.dist, now.route)); + } + } + } + + for(int i = 1; i <= N; i++) { + if (i == startNode) sb.append("- "); + else sb.append(path[i]).append(" "); + } + sb.append("\n"); + + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + + routes = new HashMap<>(); + + for(int m = 0; m < M; m++) { + st = new StringTokenizer(br.readLine()); + int s = Integer.parseInt(st.nextToken()); + int e = Integer.parseInt(st.nextToken()); + int w = Integer.parseInt(st.nextToken()); + + if (!routes.containsKey(s)) + routes.put(s, new ArrayList<>()); + if (!routes.containsKey(e)) + routes.put(e, new ArrayList<>()); + + routes.get(s).add(new Node(e, w, e)); + routes.get(e).add(new Node(s, w, s)); + } + + for(int i = 1; i <= N; i++) { + dijkstra(i); + } + + System.out.println(sb); + } +} From eeca7f1743d826e6d684b3e6f5e3fc1f80d3611f Mon Sep 17 00:00:00 2001 From: yeheun0212 Date: Tue, 25 Mar 2025 22:21:57 +0900 Subject: [PATCH 08/12] =?UTF-8?q?=EA=B7=B8=EB=9E=98=ED=94=84=20=ED=83=90?= =?UTF-8?q?=EC=83=892=5F14218.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07_graph/kim/14218.java | 64 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 07_graph/kim/14218.java diff --git a/07_graph/kim/14218.java b/07_graph/kim/14218.java new file mode 100644 index 0000000..5bb9b0d --- /dev/null +++ b/07_graph/kim/14218.java @@ -0,0 +1,64 @@ +import java.io.*; +import java.util.*; + + +public class Main { + static int N; + static Set[] routes; + static StringBuilder sb = new StringBuilder(); + + static void bfs() { + int[] path = new int[N+1]; + + PriorityQueue pq = new PriorityQueue<>((s, e) -> (s[1] - e[1])); + pq.add(new int[] {1, 0}); + + while(!pq.isEmpty()){ + int[] now = pq.poll(); + if (path[now[0]] != 0) continue; + path[now[0]] = now[1]; + + for(int r : routes[now[0]]) { + pq.add(new int[] {r, now[1]+1}); + } + } + + sb.append("0 "); + for(int i = 2; i <= N; i++) + sb.append(path[i] == 0 ? -1 : path[i]).append(" "); + sb.append("\n"); + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + routes = new HashSet[N+1]; + + for(int i = 0; i <= N; i++) + routes[i] = new HashSet<>(); + + for(int m = 0; m < M; m++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + routes[a].add(b); + routes[b].add(a); + } + + int R = Integer.parseInt(br.readLine()); + + for (int r = 0; r < R; r++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + routes[a].add(b); + routes[b].add(a); + bfs(); + } + + System.out.print(sb); + } +} From e9c63a4bbec434f0973f9d22c343fcd25ea3f51c Mon Sep 17 00:00:00 2001 From: yeheun0212 Date: Tue, 25 Mar 2025 22:22:21 +0900 Subject: [PATCH 09/12] =?UTF-8?q?=ED=83=9D=EB=B0=B0=20=EB=B0=B0=EC=86=A1?= =?UTF-8?q?=5F5972.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07_graph/kim/5972.java | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 07_graph/kim/5972.java diff --git a/07_graph/kim/5972.java b/07_graph/kim/5972.java new file mode 100644 index 0000000..8c45d27 --- /dev/null +++ b/07_graph/kim/5972.java @@ -0,0 +1,46 @@ +import java.io.*; +import java.util.*; + + +public class Main { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + + List[] routes = new ArrayList[N+1]; + for(int i = 0; i <= N; i++) { + routes[i] = new ArrayList<>(); + } + + for(int m = 0 ; m < M; m++) { + st = new StringTokenizer(br.readLine()); + int s = Integer.parseInt(st.nextToken()); + int e = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + routes[s].add(new int[] {e, c}); + routes[e].add(new int[] {s, c}); + } + + // dijkstra + boolean[] shortPath = new boolean[N+1]; + PriorityQueue pq = new PriorityQueue<>((s, e) -> s[1] - e[1]); + pq.add(new int[]{1, 0}); + shortPath[0] = true; + + while(!pq.isEmpty()) { + int[] now = pq.poll(); + if (now[0] == N) { + System.out.println(now[1]); + return; + } + if (shortPath[now[0]]) continue; + shortPath[now[0]] = true; + + for(int[] next : routes[now[0]]) { + pq.add(new int[]{next[0], now[1] + next[1]}); + } + } + } +} From b377f830fc0d277c72f5345c4877506f419f030d Mon Sep 17 00:00:00 2001 From: yeheun0212 Date: Tue, 25 Mar 2025 22:22:37 +0900 Subject: [PATCH 10/12] =?UTF-8?q?=ED=8C=80=20=ED=94=84=EB=A1=9C=EC=A0=9D?= =?UTF-8?q?=ED=8A=B8=5F9466.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07_graph/kim/9466.java | 58 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 07_graph/kim/9466.java diff --git a/07_graph/kim/9466.java b/07_graph/kim/9466.java new file mode 100644 index 0000000..080d07a --- /dev/null +++ b/07_graph/kim/9466.java @@ -0,0 +1,58 @@ +import java.io.*; +import java.util.*; + +public class Main { + static int N; + static int[] arr; + static boolean[] visited; + static boolean[] finished; + static int count; + + public static void dfs(int node) { + visited[node] = true; + int next = arr[node]; + + if (!visited[next]) + dfs(next); + else if (!finished[next]) + countCycle(next, node); + + finished[node] = true; + } + + public static void countCycle(int start, int node) { + int cnt = 1; + int cur = start; + while (cur != node) { + cnt++; + cur = arr[cur]; + } + count += cnt; + } + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringBuilder sb = new StringBuilder(); + + int T = Integer.parseInt(br.readLine()); + while (T-- > 0) { + N = Integer.parseInt(br.readLine()); + arr = new int[N + 1]; + visited = new boolean[N + 1]; + finished = new boolean[N + 1]; + count = 0; + + StringTokenizer st = new StringTokenizer(br.readLine()); + for (int i = 1; i <= N; i++) { + arr[i] = Integer.parseInt(st.nextToken()); + } + + for (int i = 1; i <= N; i++) { + if (!visited[i]) dfs(i); + } + + sb.append(N - count).append("\n"); + } + System.out.print(sb); + } +} From 036b1f1d5908cc62365cb7c52623e8c4b871ddaa Mon Sep 17 00:00:00 2001 From: yeheun0212 Date: Tue, 25 Mar 2025 22:22:56 +0900 Subject: [PATCH 11/12] =?UTF-8?q?=EB=82=98=EC=9D=B4=ED=8A=B8=EC=9D=98=20?= =?UTF-8?q?=EC=9D=B4=EB=8F=99=5F10026.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07_graph/kim/10026.java | 80 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 07_graph/kim/10026.java diff --git a/07_graph/kim/10026.java b/07_graph/kim/10026.java new file mode 100644 index 0000000..14e80d0 --- /dev/null +++ b/07_graph/kim/10026.java @@ -0,0 +1,80 @@ +import java.io.*; +import java.util.*; + + +public class Main { + static int N; + static char[][] map; + static boolean[][] visited; + static int[][] move = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; + static int result = 0; + static int resultColor = 0; + + static void dfs(int n, int m) { + if(!visited[n][m]) return; + + visited[n][m] = false; + + for(int[] mv : move) { + int nextn = n + mv[0]; + int nextm = m + mv[1]; + + if (nextn < 0 || nextn >= N || nextm < 0 || nextm >= N) continue; + + if (map[n][m] != 'B' && map[nextn][nextm] != 'B') dfs(nextn, nextm); + else if (map[n][m] == 'B' && map[nextn][nextm] == 'B') dfs(nextn, nextm); + } + } + + static void dfsColor(int n, int m) { + if(visited[n][m]) return; + + visited[n][m] = true; + + for(int[] mv : move) { + int nextn = n + mv[0]; + int nextm = m + mv[1]; + + if (nextn < 0 || nextn >= N || nextm < 0 || nextm >= N) continue; + if (map[n][m] != map[nextn][nextm]) continue; + + dfsColor(nextn, nextm); + } + } + + public static void main(String[] args) throws Exception { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + N = Integer.parseInt(br.readLine()); + + map = new char[N][N]; + visited = new boolean[N][N]; + + for(int i = 0; i < N; i++) { + String line = br.readLine(); + for(int j = 0; j < N; j++) { + map[i][j] = line.charAt(j); + } + } + + for(int i = 0; i < N; i++) { + for(int j = 0; j < N; j++) { + if(!visited[i][j]) { + resultColor ++; + dfsColor(i, j); + } + } + } + + for(int i = 0; i < N; i++) { + for(int j = 0; j < N; j++) { + if(visited[i][j]) { + result ++; + dfs(i, j); + } + } + } + + System.out.println(resultColor + " " + result); + } +} From deef17088729be063b85bd0c2b99ee4137d32572 Mon Sep 17 00:00:00 2001 From: yeheun0212 Date: Tue, 25 Mar 2025 22:23:15 +0900 Subject: [PATCH 12/12] =?UTF-8?q?=EC=A0=81=EB=A1=9D=EC=83=89=EC=95=BD=5F75?= =?UTF-8?q?62.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 07_graph/kim/7562.java | 52 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 07_graph/kim/7562.java diff --git a/07_graph/kim/7562.java b/07_graph/kim/7562.java new file mode 100644 index 0000000..b726b1e --- /dev/null +++ b/07_graph/kim/7562.java @@ -0,0 +1,52 @@ +import java.io.*; +import java.util.*; + + +public class Main { + + static int N; + static int[][] map; + static int[] start; + static int[] end; + + static int[][] move = {{1,-2}, {-1,-2}, {2,-1}, {-2,-1}, {2,1}, {-2,1}, {1,2}, {-1,2}}; + + static void bfs() { + Queue queue = new LinkedList<>(); + queue.add(start); + + while(!queue.isEmpty()) { + int[] now = queue.poll(); + + if (now[0] == end[0] && now[1] == end[1]) break; + + for(int[] mv : move) { + int nextn = now[0] + mv[0]; + int nextm = now[1] + mv[1]; + + if (nextn < 0 || nextm < 0 || nextn >= N || nextm >= N) continue; + if (map[nextn][nextm] != 0) continue; + map[nextn][nextm] = map[now[0]][now[1]] + 1; + queue.add(new int[] {nextn, nextm}); + } + } + System.out.println(map[end[0]][end[1]]); + } + + public static void main(String[] args) throws IOException{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + int T = Integer.parseInt(br.readLine()); + + for(int t=0; t < T; t++) { + N = Integer.parseInt(br.readLine()); + map = new int[N][N]; + StringTokenizer st = new StringTokenizer(br.readLine()); + start = new int[] {Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())}; + st = new StringTokenizer(br.readLine()); + end = new int[] {Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken())}; + + bfs(); + } + } +}