Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions Java/임건애/[Week 14]DFS,BFS/[BOJ] DFS와 BFS_1260_실버2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import java.util.*;
import java.io.*;

class Main
{
static int N, M, V;
static int[][] graph;
static boolean[] visited;
static StringBuilder sb;

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());
M = Integer.parseInt(st.nextToken());
V = Integer.parseInt(st.nextToken());

graph = new int[N + 1][N + 1];

// 그래프 정보 저장
for (int i = 0; i < M; i++) {
st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());

graph[a][b] = 1;
graph[b][a] = 1;
}

visited = new boolean[N + 1];
sb = new StringBuilder();
DFS(V);
System.out.println(sb.toString());

visited = new boolean[N + 1];
sb = new StringBuilder();
BFS(V);
System.out.println(sb.toString());
}


public static void DFS(int start) {
visited[start] = true;
sb.append(start).append(' ');

for (int next = 1; next <= N; next++) {
if (graph[start][next] == 1 && !visited[next]) {
DFS(next);
}
}
}

public static void BFS(int start) {
Queue<Integer> q = new LinkedList<>();
visited[start] = true;
q.add(start);

while (!q.isEmpty()) {
int n = q.poll();
sb.append(n).append(' ');

for (int next = 1; next <= N; next++) {
if (graph[n][next] == 1 && !visited[next]) {
visited[next] = true;
q.add(next);
}
}
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import java.io.*;
import java.util.*;

class Main {
static final int MAX = 100000;

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;
}

boolean[] visited = new boolean[MAX + 1];
int[] arr = new int[MAX + 1];

Queue<Integer> q = new LinkedList<>();
visited[N] = true;
q.add(N);

while (!q.isEmpty()) {
int start = q.poll();

if (start == K) {
System.out.println(arr[start]);
return;
}

int[] next = {start - 1, start + 1, start * 2};
for (int n : next) {
if (0 <= n && n <= MAX && !visited[n]) {
visited[n] = true;
arr[n] = arr[start] + 1;
q.add(n);
}
}
}

}

}
25 changes: 25 additions & 0 deletions Java/임건애/[Week 14]DFS,BFS/[PGS] 네트워크_43162_LV3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public int solution(int n, int[][] computers) {
int answer = 0;
boolean[] visited = new boolean[n];

for (int i = 0; i < n; i++) {
if (!visited[i]) {
DFS(i, n, computers, visited);
answer++;
}
}

return answer;
}

private void DFS(int start, int n, int[][] computers, boolean[] visited) {
visited[start] = true;
for (int next = 0; next < n; next++) {
if (computers[start][next] == 1 && !visited[next]) {
DFS(next, n, computers, visited);
}
}
}

}