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 | 29 | 30 | 31 |
Tags
- SQL
- 개발자 면접 준비
- 프로그래머스 이중우선순위큐 java
- 이중우선순위큐 java
- JPA
- Gradle
- 디베이스앤 인턴 후기
- oracle ansi
- 프로그래머스 이중우선순위큐 자바
- 면접 필수 질문
- BFS
- 그리디
- 위상정렬
- CJ DBASE&
- DP
- Java
- 프로그래머스 이중우선순위큐
- 백준
- 백트래킹
- 이분탐색
- ansi sql 단점
- DFS
- 디베이스앤
- DBASE&
- Spring Boot
- oracle ansi sql
- 프로그래머스
- 이중우선순위큐 자바
- IT 면접 준비
- ansi sql 장점
Archives
- Today
- Total
쉬운 프로그래밍
[알고리즘] 프로그래머스 - 네트워크 본문
programmers.co.kr/learn/courses/30/lessons/43162
import java.util.*;
class Solution {
public int solution(int n, int[][] computers) {
boolean visited[] = new boolean[n];
int answer = 0;
for (int i = 0; i < n; i++) {
if (!visited[i]) {
answer += bfs(computers, visited, n, i);
}
}
return answer;
}
public int bfs(int[][] computers, boolean[] visited, int n, int start) {
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(start);
visited[start] = true;
while (!queue.isEmpty()) {
int temp = queue.poll();
for (int i = 0; i < n; i++) {
if (computers[temp][i] == 1 && !visited[i]) {
queue.add(i);
visited[i] = true;
}
}
}
return 1;
}
}
어렵게 생각할 것 없이 간선이 서로 연결된 그래프의 덩어리 개수를 구하는 문제이다.
주어진 인접행렬을 가지고 모든 정점에 대해서 bfs함수가 돌아간 횟수를 구하면 된다.
단순 그래프 문제라서 visited를 2차원 배열로 만들 필요도 없이 1차원이면 된다.
'알고리즘 > DFS, BFS' 카테고리의 다른 글
[알고리즘] 백준 1697 숨바꼭질 - JAVA (0) | 2021.03.04 |
---|---|
[알고리즘] 백준 2146 다리만들기 (0) | 2021.03.03 |
[알고리즘] 프로그래머스 - 타겟 넘버 (0) | 2021.01.25 |
[알고리즘] 백준 4179 불 (0) | 2021.01.24 |
[알고리즘] 백준 2583 영역 구하기 (0) | 2021.01.23 |
Comments