본문 바로가기

DFS4

[프로그래머스, kotlin] 타겟넘버 재귀를 이용한 문제 풀이 fun solution(numbers: IntArray, target: Int): Int { var answer=0 fun dfs(index: Int, currentSum: Int) { if (index == numbers.size) { if (currentSum == target) { answer = answer + 1 } } else { val add = dfs(index + 1, currentSum + numbers[index]) val subtract = dfs(index + 1, currentSum - numbers[index]) .. 2024. 6. 28.
[프로그래머스 , kotlin] 피로도 연결리스트를 이용한 풀이 ( 스택과 같은 기능으로)fun solution_ff(k: Int, dungeons: Array): Int { val stack = mutableListOf(Triple(k, 0, BooleanArray(dungeons.size))) var maxCount = 0 while (stack.isNotEmpty()) { val (currentK, count, visited) = stack.removeAt(stack.lastIndex) maxCount = maxOf(maxCount, count) for (i in dungeons.indices) { if (!visited[i] && currentK >= dungeo.. 2024. 6. 27.
깊이 우선 탐색(DFS, Depth-First Search) 이란? 깊이 우선 탐색(DFS, Depth-First Search)깊이 우선 탐색 (DFS)는 하나의 순환 알고리즘으로 백트래킹에 사용하는 대표적인 탐색 알고리즘이다. 루트 노드(혹은 다른 임의의 노드)에서 시작해서 다음 분기로 넘어가기 전에 해당 분기를 완벽하게 탐색하는 방식을 말한다. 주로, 재귀함수 또는 Stack으로 구현할 수 있다.  깊이 우선 탐색의 구현1. 순환 호출 이용def dfs_recursive(graph, start, visited = []):## 데이터를 추가하는 명령어 / 재귀가 이루어짐 visited.append(start) for node in graph[start]: if node not in visited: dfs_recursive(.. 2024. 6. 12.
[kotlin] 백준 1280 DFS와 BFS package com.example.codingtestimport java.io.BufferedReaderimport java.io.InputStreamReaderimport java.util.LinkedListimport java.util.Queueimport java.util.Stackfun main() { val br = BufferedReader(InputStreamReader(System.`in`)) val (n, m,v) = readLine()!!.split(" ").map { it.toInt() } val graph = Array(1001, { IntArray(10001, { 0 }) }) // Array(크기, {IntArray(크기, {초기화 값})} val v.. 2023. 10. 27.