| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
- DP
- Kafka
- 오라클
- Intellj
- Jenkins
- 운영체제
- Spring Cloud Feign
- 쿼리
- Spring
- db
- SQL
- JPA
- MST
- Spring Boot
- 클라우드
- 디자인 패턴
- 알고리즘
- golang
- 페이징
- PL/SQL
- 자료구조
- aws
- 자바
- retry
- 데이터베이스
- Spring Cloud
- feign
- 코딩
- 백준
- MVC
- Today
- Total
justgo_developer
[JAVA] 백준 1260 DFS와 BFS 본문
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static int N;
static int M;
static int V;
static int[][]G;
static boolean[]visit;
public static void dfs(int n){
visit[n]=true;
System.out.print(n+" ");
for(int i=1; i<=N;i++){
if(G[n][i]==1 && visit[i]==false){
dfs(i);
}
}
}
public static void bfs(int n){
Queue<Integer> q = new LinkedList<Integer> ();
q.offer(n);
visit[n]=true;
int temp;
while(!q.isEmpty()){
temp = q.poll();
System.out.print(temp+" ");
for(int i=0;i<=N;i++){
if(G[temp][i]==1 && visit[i]==false){
q.offer(i);
visit[i]=true;
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
M = sc.nextInt();
V = sc.nextInt();
G = new int[1001][1001];
visit = new boolean[10001];
int t1,t2;
for(int i=0;i<M;i++){
t1 = sc.nextInt();
t2 = sc.nextInt();
G[t1][t2] = G[t2][t1] =1;
}
dfs(V);
for(int i=1;i<=N;i++)
visit[i]=false;
System.out.println("");
bfs(V);
}
}
'IT > 알고리즘' 카테고리의 다른 글
| 최단경로(shortest path problem)-3 (0) | 2018.01.11 |
|---|---|
| 최단경로(shortest path problem)-2 (0) | 2018.01.10 |
| 최단경로(shortest path problem)-1 (0) | 2018.01.06 |
| 최소비용신장트리(minimum spanning tree)-3 (0) | 2018.01.05 |
| 최소비용신장트리(minimum spanning tree)-2 (0) | 2018.01.04 |