diff --git a/bona1122/[week7]Graph/Delivery_route.js b/bona1122/[week7]Graph/Delivery_route.js new file mode 100644 index 0000000..d7a111b --- /dev/null +++ b/bona1122/[week7]Graph/Delivery_route.js @@ -0,0 +1,33 @@ +// 가중치 있는 무방향 그래프(중복간선 존재) +// 다익스트라 알고리즘 활용 +// 1번에서 각 마을로 음식배달. K시간 이하로 배달 가능한 곳 개수 구하기 + +const solution = (N, road, K) => { + const graph = Array.from({ length: N + 1 }, () => []) + for (let [a, b, w] of road) { + graph[a].push([b, w]) + graph[b].push([a, w]) + } + + const distance = Array(N + 1).fill(Infinity) + distance[1] = 0 + + const pq = [[0, 1]] // [거리, 노드] + + while (pq.length > 0) { + pq.sort((a, b) => a[0] - b[0]) // 매번 sort보다 우선순위큐 구현해서 사용하는 것이 베스트 + const [dist, cur] = pq.shift() + + if (distance[cur] < dist) continue + + for (const [next, w] of graph[cur]) { + const cost = dist + w + if (cost < distance[next]) { + distance[next] = cost + pq.push([cost, next]) + } + } + } + + return distance.filter((dist) => dist <= K).length +} \ No newline at end of file diff --git a/bona1122/[week7]Graph/Maze_escape.js b/bona1122/[week7]Graph/Maze_escape.js new file mode 100644 index 0000000..8cb976c --- /dev/null +++ b/bona1122/[week7]Graph/Maze_escape.js @@ -0,0 +1,52 @@ +// 출발점부터 빠르게 레버있는 곳으로 간 후, 다시 레버에서 탈출구로 가는 것 목표. 못가면 -1 반환 +const solution = (maps) => { + const dir = [ + [0, 1], + [0, -1], + [1, 0], + [-1, 0], + ] + const n = maps.length + const m = maps[0].length + let S, L, E + // 시작, 레버, 출구 위치 저장하고 + // 시작 - 레버 / 레버 - 출구 경로의 최단경로 더하기 + maps = maps.map((row, x) => { + row = row.split("") + row.forEach((item, y) => { + if (item === "S") S = [x, y] + if (item === "L") L = [x, y] + if (item === "E") E = [x, y] + }) + return row + }) + + const bfs = (start, end) => { + const visited = Array.from({ length: n }, () => Array(m).fill(false)) + const q = [[...start, 0]] + + while (q.length) { + let [x, y, dist] = q.shift() + if (x === end[0] && y === end[1]) return dist + + for (let [dx, dy] of dir) { + let [nx, ny] = [x + dx, y + dy] + if (nx >= 0 && nx < n && ny >= 0 && ny < m) { + if (!visited[nx][ny] && maps[nx][ny] !== "X") { + visited[nx][ny] = true + q.push([nx, ny, dist + 1]) + } + } + } + } + + return -1 + } + + let toL = bfs(S, L) + if (toL === -1) return -1 + let toE = bfs(L, E) + if (toE === -1) return -1 + + return toL + toE +} diff --git a/bona1122/[week7]Graph/Power_grid_division.js b/bona1122/[week7]Graph/Power_grid_division.js new file mode 100644 index 0000000..a354bd8 --- /dev/null +++ b/bona1122/[week7]Graph/Power_grid_division.js @@ -0,0 +1,35 @@ +// n개의 송전탑이 트리 형태 +// 하나를 끊어서 2개로 분할하고자 함. 최대한 갯수 같게. +// 와이어중에 하나 끊은다음에, 하나에 대해 bfs수행하기 +const solution = (n, wires) => { + let diff = n + const graph = Array.from({ length: n + 1 }, () => []) + for ([a, b] of wires) { + graph[a].push(b) + graph[b].push(a) + } + // 송전선 별로 끊어보기(완전탐색) + for ([a, b] of wires) { + const visited = new Array(n + 1).fill(false) + visited[b] = true // b로는 못가도록 처리 + + let cnt = 0 + const q = [a] + visited[a] = true + + while (q.length) { + let cur = q.shift() + cnt++ + + for (let next of graph[cur]) { + if (!visited[next]) { + visited[next] = true + q.push(next) + } + } + } + + diff = Math.min(diff, Math.abs(cnt - (n - cnt))) // 전력망 노드 차이 갱신 + } + return diff +} diff --git a/bona1122/[week7]Graph/Shortest_path_game.js b/bona1122/[week7]Graph/Shortest_path_game.js new file mode 100644 index 0000000..141bbb9 --- /dev/null +++ b/bona1122/[week7]Graph/Shortest_path_game.js @@ -0,0 +1,32 @@ +const solution = (maps) => { + const n = maps.length + const m = maps[0].length + const dir = [ + [1, 0], + [-1, 0], + [0, -1], + [0, 1], + ] + const visited = Array.from({ length: n }, () => Array(m).fill(false)) + visited[0][0] = true + const q = [[0, 0, 1]] // [x,y,거리] 저장 + + while (q.length) { + let [x, y, depth] = q.shift() + if (x === n - 1 && y === m - 1) { + return depth + } + for (let [dx, dy] of dir) { + let nx = x + dx + let ny = y + dy + if (nx >= 0 && nx < n && ny >= 0 && ny < m) { + if (maps[nx][ny] === 1 && !visited[nx][ny]) { + visited[nx][ny] = true + q.push([nx, ny, depth + 1]) + } + } + } + } + + return -1 +} diff --git a/bona1122/[week7]Graph/Target_number.js b/bona1122/[week7]Graph/Target_number.js new file mode 100644 index 0000000..fee51e9 --- /dev/null +++ b/bona1122/[week7]Graph/Target_number.js @@ -0,0 +1,38 @@ +// DFS 풀이 +function solution(numbers, target) { + let result = 0 + + const dfs = (acc, depth) => { + if (depth === numbers.length) { + if (acc === target) result++ + return + } + dfs(acc + numbers[depth], depth + 1) + dfs(acc - numbers[depth], depth + 1) + } + + dfs(0, 0) + return result +} + +// BFS 풀이 +function solution(numbers, target) { + let result = 0 + const queue = [] + + queue.push([0, 0]) // [합계, 처리할 인덱스] + + while (queue.length) { + const [acc, depth] = queue.shift() + + if (depth === numbers.length) { + if (acc === target) result++ + continue + } + + queue.push([acc + numbers[depth], depth + 1]) + queue.push([acc - numbers[depth], depth + 1]) + } + + return result +}