FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-for-coding-test/5/9.java at master · ndb796/python-for-coding-test · GitHub
ndb796
/
python-for-coding-test
Public
Notifications
You must be signed in to change notification settings
Fork
831
Star
2.4k
Code
Issues
129
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-for-coding-test
/
5
/
9.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
73 lines (59 loc) · 2.12 KB
Breadcrumbs
python-for-coding-test
/
5
/
9.java
Copy path
File metadata and controls
73 lines (59 loc) · 2.12 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import
java
.
util
.*;
public
class
Main
{
public
static
boolean
[]
visited
=
new
boolean
[
9
];
public
static
ArrayList
<
ArrayList
<
Integer
>>
graph
=
new
ArrayList
<
ArrayList
<
Integer
>>();
// BFS 함수 정의
public
static
void
bfs
(
int
start
) {
Queue
<
Integer
>
q
=
new
LinkedList
<>();
q
.
offer
(
start
);
// 현재 노드를 방문 처리
visited
[
start
] =
true
;
// 큐가 빌 때까지 반복
while
(!
q
.
isEmpty
()) {
// 큐에서 하나의 원소를 뽑아 출력
int
x
=
q
.
poll
();
System
.
out
.
print
(
x
+
" "
);
// 해당 원소와 연결된, 아직 방문하지 않은 원소들을 큐에 삽입
for
(
int
i
=
0
;
i
<
graph
.
get
(
x
).
size
();
i
++) {
int
y
=
graph
.
get
(
x
).
get
(
i
);
if
(!
visited
[
y
]) {
q
.
offer
(
y
);
visited
[
y
] =
true
;
}
}
}
}
public
static
void
main
(
String
[]
args
) {
// 그래프 초기화
for
(
int
i
=
0
;
i
<
9
;
i
++) {
graph
.
add
(
new
ArrayList
<
Integer
>());
}
// 노드 1에 연결된 노드 정보 저장
graph
.
get
(
1
).
add
(
2
);
graph
.
get
(
1
).
add
(
3
);
graph
.
get
(
1
).
add
(
8
);
// 노드 2에 연결된 노드 정보 저장
graph
.
get
(
2
).
add
(
1
);
graph
.
get
(
2
).
add
(
7
);
// 노드 3에 연결된 노드 정보 저장
graph
.
get
(
3
).
add
(
1
);
graph
.
get
(
3
).
add
(
4
);
graph
.
get
(
3
).
add
(
5
);
// 노드 4에 연결된 노드 정보 저장
graph
.
get
(
4
).
add
(
3
);
graph
.
get
(
4
).
add
(
5
);
// 노드 5에 연결된 노드 정보 저장
graph
.
get
(
5
).
add
(
3
);
graph
.
get
(
5
).
add
(
4
);
// 노드 6에 연결된 노드 정보 저장
graph
.
get
(
6
).
add
(
7
);
// 노드 7에 연결된 노드 정보 저장
graph
.
get
(
7
).
add
(
2
);
graph
.
get
(
7
).
add
(
6
);
graph
.
get
(
7
).
add
(
8
);
// 노드 8에 연결된 노드 정보 저장
graph
.
get
(
8
).
add
(
1
);
graph
.
get
(
8
).
add
(
7
);
bfs
(
1
);
}
}
Back
|
FazBrowse Home
|
New Git URL