일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- dfs
- PostgreSQL
- process.env
- AOP
- git
- CSS
- dotenv
- package
- 메모이제이션
- Solid
- Java
- npm
- bfs
- 클라우드
- 동적계획법
- 추상화
- 객체지향
- netlify
- MariaDB
- mock
- 상속
- github
- Secret
- GOF
- 디자인 패턴
- 캡슐화
- 다형성
- azure
- DP
- 서브셋폰트
Archives
- Today
- Total
이것저것 해보기🌼
[DFS/BFS/백트래킹] 참고자료, 코드 본문
1) DFS / BFS
DFS 와 BFS 개념과 알고리즘 구현에 대해 깔끔하게 정리된 글이 있어 첨부한다.
https://covenant.tistory.com/132
DFS BFS란? 백준 문제추천
DFS BFS란? 백준 문제추천 그래프의 모든 노드를 방문 하는 알고리즘으로 DFS와 BFS가 있습니다. 어려운 코딩테스트를 통과하고 나면 만나게 될 기업 기술 면접의 단골 주제입니다. 본 알고리즘에
covenant.tistory.com
2) 백트래킹 참고자료
https://zoonvivor.tistory.com/105
모든 경우의 수 (백 트래킹)
우연히 백트래킹 공부하다가 이전에 비트연산으로 만들었던 경우의 수를 백트래킹으로 구현해보았다. 백트래킹은 다른 DFS와는 다르게 함수안에 반복문이 구현되어있다. 문제 이해 하고 설명
zoonvivor.tistory.com
내 코드
1) DFS
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
class Solution {
public int solution(int n, int[][] computers) {
int answer = 0;
boolean[] visited = new boolean[n];
for(int i= 0; i<n; i++){
if(visited[i]==false){ //방문하지 않은 노드
dfs(computers, visited, i, n);
//System.out.println(i);
answer++;
}
}
return answer;
}
public void dfs (int[][] computers, boolean[] visited, int a, int n){
visited[a] = true;
for(int i= 0; i<n; i++){ // 모든 인접노드 찾기
if(computers[a][i]==1 && visited[i] == false){
dfs(computers, visited, i, n);
}
}
}
}
|
cs |
2) 백트래킹 (모든 경우의 수 찾기)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class Solution {
public void solution(int[] numbers, int target) {
int[] arr = new int [numbers.length];
int n = numbers.length;
dfs(numbers, arr, 0,0,n,target);
}
static void dfs(int[] numbers, int[] arr, int start, int depth, int n, int target) {
for(int i=start; i< n ; i++){
arr[i] = 1;
dfs(numbers, arr, i+1, depth+1, n, target); //다음 index부터 마지막 index까지
arr[i] = 0; //백트래킹. 현재 index에 대한 다른 케이스
}
print(arr);
}
}
|
cs |
위 결과에서 arr 를 print 하면
1 1 1 1 0
1 1 1 0 1
1 1 1 0 0
....
이런식으로 1과 0을 사용해서 나올수 있는 모든 경우의 수가 나온다.
'코딩테스트' 카테고리의 다른 글
[백준 온라인 저지] 1991번: 트리 순회 (0) | 2021.07.02 |
---|---|
[SQL] postgreSQL 문법 정리(CASE,JOIN,GROUP BY등) (0) | 2021.07.02 |
[구름] 코딩테스트 대비 JAVA BufferedReader 사용법 (0) | 2021.07.02 |
[프로그래머스] 타겟 넘버 (0) | 2021.07.02 |
[프로그래머스] 기능개발 (0) | 2021.06.30 |