250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 완전탐색
- 코테준비
- 자바
- 정올 1620
- Bfs와DFS
- 자바 코테
- 삼성역테
- 파티션 크기 조정
- 정보처리기사
- 알고리즘
- 전화번호속의암호
- 백준13458
- 백준2251
- 코테
- 순열
- BFS
- 완탐
- 중복순열
- 백준15652
- 재귀함수
- 중복조합
- 알고리즘개념
- 에라토스테네스의채
- 백준
- java
- D드라이브생성
- 주사위굴리기2
- N과M
- 볼륨 만들기
- 23288
Archives
- Today
- Total
뚱땅뚱땅
[문제] 백준 1260번 BFS와 DFS 본문
728x90
* 출처
1260번: DFS와 BFS
첫째 줄에 정점의 개수 N(1 ≤ N ≤ 1,000), 간선의 개수 M(1 ≤ M ≤ 10,000), 탐색을 시작할 정점의 번호 V가 주어진다. 다음 M개의 줄에는 간선이 연결하는 두 정점의 번호가 주어진다. 어떤 두 정점 사
www.acmicpc.net
내 풀이 (1)
인접리스트를 이용해서 그래프를 표현했다.
public class BOJ_1260 {
static int N;
static ArrayList<ArrayList<Integer>> list;
static boolean[] visited;
static StringBuilder sdfs = new StringBuilder();
static StringBuilder sbfs = new StringBuilder();
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine()," ");
N = Integer.parseInt(st.nextToken()); // 정점 개수
int M = Integer.parseInt(st.nextToken()); // 간선 개수
int V = Integer.parseInt(st.nextToken()); // 탐색 시작 정점 번호
list = new ArrayList<>();
for(int i=0;i<N+1;i++) { // 1 based
list.add(new ArrayList<>());
}
for(int i=0;i<M;i++) {
st = new StringTokenizer(in.readLine()," ");
int p1 = Integer.parseInt(st.nextToken());
int p2 = Integer.parseInt(st.nextToken());
list.get(p1).add(p2);
list.get(p2).add(p1);
}
for(int i=1;i<=N;i++) {
list.get(i).sort(null);
}
int size = list.get(V).size();
visited = new boolean[N+1];
dfs(V, size);
visited = new boolean[N+1];
bfs(V);
System.out.println(sdfs);
System.out.println(sbfs);
}
static void dfs(int target, int size) {
if(visited[target]) return;
visited[target] = true;
sdfs.append(target).append(" ");
for(int i=0;i<size;i++) {
int x = list.get(target).get(i);
if(!visited[x])
dfs(x,list.get(x).size());
}
}
static void bfs(int target) {
visited[target] = true;
Queue<Integer> q = new LinkedList<>();
q.offer(target);
while(!q.isEmpty()) {
int curr = q.poll();
sbfs.append(curr).append(" ");
for(int i=0;i<list.get(curr).size();i++) {
int tmp = list.get(curr).get(i);
if(visited[tmp]) continue;
visited[tmp] = true;
q.offer(tmp);
}
}
}
}
풀이 (2)
인접 행렬을 이용한 구현
public class Main {
static int N, M, V;
static int[][] matrix;
static StringBuilder strDfs = new StringBuilder();
static StringBuilder strBfs = new StringBuilder();
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine()," ");
N = Integer.parseInt(st.nextToken()); // 정점 개수
M = Integer.parseInt(st.nextToken()); // 간선 개수
V = Integer.parseInt(st.nextToken()); // 시작 정점 번호
matrix = new int[N+1][N+1]; // index가 1 based임 (정점번호: 1~N번)
for(int i=0;i<M;i++) {
st = new StringTokenizer(in.readLine()," ");
int from = Integer.parseInt(st.nextToken());
int to = Integer.parseInt(st.nextToken());
// 간선이 양방향으로 주어지기 때문에 둘다 표시해준다.
matrix[from][to]= 1;
matrix[to][from]= 1;
}
// dfs
dfs(V, new boolean[N+1]);
System.out.println(strDfs);
// bfs
bfs(V);
System.out.println(strBfs);
}
static void dfs(int start, boolean[] visited) {
visited[start] = true;
strDfs.append(start).append(' ');
for(int i=1;i<=N;i++) {
if((matrix[start][i] == 1) &&!visited[i]) {
dfs(i, visited);
}
}
}
static void bfs(int start) {
boolean[] visited = new boolean[N+1];
Queue<Integer> q = new LinkedList<>();
q.add(start);
visited[start] = true;
while(!q.isEmpty()) {
int node = q.poll();
strBfs.append(node).append(' ');
for(int i=1;i<=N;i++) {
if(matrix[node][i]==1 && !visited[i]) {
visited[i] = true;
q.add(i);
}
}
}
}
}
728x90
'알고리즘 > 백준' 카테고리의 다른 글
[문제] 백준 13458번 시험 감독 (0) | 2021.03.08 |
---|---|
[문제] 백준 1149번 RGB 거리 (0) | 2021.03.06 |
[문제] 백준 10026번 적록색약 (0) | 2021.03.03 |
[문제] 백준 2491번 수열 (0) | 2021.02.18 |
[문제] 백준 15686번 치킨 배달 (0) | 2021.02.17 |
Comments