Skip to content

Commit 6ff1654

Browse files
authored
BFS 모음 (#80)
* 효빈이의 과외 * 숨바꼭질 * 숨바꼭질 * 케빈 베이컨의 6단계 법칙
1 parent 79cab6e commit 6ff1654

3 files changed

Lines changed: 193 additions & 0 deletions

File tree

pr/숨바꼭질/Main.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
static int n;
5+
static int k;
6+
static int[] visited;
7+
8+
public static void main(String[] args) {
9+
10+
Scanner sc = new Scanner(System.in);
11+
n = sc.nextInt();
12+
k = sc.nextInt();
13+
14+
visited = new int[100001];
15+
16+
Arrays.fill(visited, -1);
17+
18+
solve();
19+
20+
}
21+
22+
public static void solve() {
23+
24+
if (n == k) {
25+
System.out.println(0);
26+
return;
27+
}
28+
29+
Queue<Integer> queue = new LinkedList<>();
30+
31+
visited[n] = 1;
32+
queue.add(n);
33+
34+
while (!queue.isEmpty()) {
35+
int current = queue.poll();
36+
int[] m = { current + 1, current - 1, current * 2 };
37+
for (int x = 0; x < 3; x++) {
38+
int next = m[x];
39+
if (next < 0)
40+
continue;
41+
if (next > 100000)
42+
continue;
43+
44+
// 이미 방문했었용
45+
if (visited[next] != -1)
46+
continue;
47+
if (next == k) {
48+
System.out.println(visited[current]);
49+
return;
50+
}
51+
visited[next] = visited[current] + 1;
52+
queue.add(next);
53+
54+
}
55+
}
56+
57+
}
58+
59+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
static int n;
5+
static int m;
6+
7+
static int[][] friends;
8+
9+
public static void main(String[] args) {
10+
11+
Scanner sc = new Scanner(System.in);
12+
n = sc.nextInt();
13+
m = sc.nextInt();
14+
15+
friends = new int[n + 1][n + 1];
16+
17+
for (int i = 0; i < m; i++) {
18+
int a = sc.nextInt();
19+
int b = sc.nextInt();
20+
friends[a][b] = 1;
21+
friends[b][a] = 1;
22+
}
23+
24+
solve();
25+
26+
}
27+
28+
public static void solve() {
29+
int min = Integer.MAX_VALUE;
30+
int index = 0;
31+
32+
for (int i = 1; i <= n; i++) {
33+
int length = bfs(i);
34+
35+
if (length < min) {
36+
min = length;
37+
index = i;
38+
}
39+
}
40+
System.out.println(index);
41+
}
42+
43+
public static int bfs(int start) {
44+
Queue<Integer> q = new LinkedList<>();
45+
boolean[] friendShip = new boolean[n + 1];
46+
int[] dist = new int[n + 1];
47+
Arrays.fill(dist, -1);
48+
q.add(start);
49+
friendShip[start] = true;
50+
dist[start] = 0;
51+
52+
while (!q.isEmpty()) {
53+
int current = q.poll();
54+
55+
for (int other = 1; other <= n; other++) {
56+
if (friendShip[other])
57+
continue;
58+
// 친구가 아니라면 넘어갑니다.
59+
if (friends[current][other] == 0)
60+
continue;
61+
62+
friendShip[other] = true;
63+
dist[other] = dist[current] + 1;
64+
q.add(other);
65+
}
66+
67+
}
68+
int ans = 0;
69+
for (int i = 1; i <= n; i++) {
70+
ans += dist[i];
71+
}
72+
return ans;
73+
}
74+
75+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import java.util.*;
2+
3+
public class Main {
4+
static int n;
5+
static int k;
6+
static int[] visited;
7+
8+
public static void main(String[] args) {
9+
10+
Scanner sc = new Scanner(System.in);
11+
n = sc.nextInt();
12+
k = sc.nextInt();
13+
14+
visited = new int[100001];
15+
16+
Arrays.fill(visited, -1);
17+
18+
solve();
19+
20+
}
21+
22+
public static void solve() {
23+
24+
if (n == k) {
25+
System.out.println(0);
26+
return;
27+
}
28+
29+
Queue<Integer> queue = new LinkedList<>();
30+
31+
visited[n] = 1;
32+
queue.add(n);
33+
34+
while (!queue.isEmpty()) {
35+
int current = queue.poll();
36+
int[] m = { current + 1, current - 1, current * 2 };
37+
for (int x = 0; x < 3; x++) {
38+
int next = m[x];
39+
if (next < 0)
40+
continue;
41+
if (next > 100000)
42+
continue;
43+
44+
// 이미 방문했었용
45+
if (visited[next] != -1)
46+
continue;
47+
if (next == k) {
48+
System.out.println(visited[current]);
49+
return;
50+
}
51+
visited[next] = visited[current] + 1;
52+
queue.add(next);
53+
54+
}
55+
}
56+
57+
}
58+
59+
}

0 commit comments

Comments
 (0)