Skip to content

Commit b4692f5

Browse files
authored
[Bona1122] 25.02.20 (#49)
* Shortest path game / 중급 * Target number / 중급 * Power grid division / 심화 * Delivery route / 심화 * Maze escape / 심화 * fix Delivery_route.js
1 parent 619ff7c commit b4692f5

File tree

5 files changed

+190
-0
lines changed

5 files changed

+190
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// 가중치 있는 무방향 그래프(중복간선 존재)
2+
// 다익스트라 알고리즘 활용
3+
// 1번에서 각 마을로 음식배달. K시간 이하로 배달 가능한 곳 개수 구하기
4+
5+
const solution = (N, road, K) => {
6+
const graph = Array.from({ length: N + 1 }, () => [])
7+
for (let [a, b, w] of road) {
8+
graph[a].push([b, w])
9+
graph[b].push([a, w])
10+
}
11+
12+
const distance = Array(N + 1).fill(Infinity)
13+
distance[1] = 0
14+
15+
const pq = [[0, 1]] // [거리, 노드]
16+
17+
while (pq.length > 0) {
18+
pq.sort((a, b) => a[0] - b[0]) // 매번 sort보다 우선순위큐 구현해서 사용하는 것이 베스트
19+
const [dist, cur] = pq.shift()
20+
21+
if (distance[cur] < dist) continue
22+
23+
for (const [next, w] of graph[cur]) {
24+
const cost = dist + w
25+
if (cost < distance[next]) {
26+
distance[next] = cost
27+
pq.push([cost, next])
28+
}
29+
}
30+
}
31+
32+
return distance.filter((dist) => dist <= K).length
33+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// 출발점부터 빠르게 레버있는 곳으로 간 후, 다시 레버에서 탈출구로 가는 것 목표. 못가면 -1 반환
2+
const solution = (maps) => {
3+
const dir = [
4+
[0, 1],
5+
[0, -1],
6+
[1, 0],
7+
[-1, 0],
8+
]
9+
const n = maps.length
10+
const m = maps[0].length
11+
let S, L, E
12+
// 시작, 레버, 출구 위치 저장하고
13+
// 시작 - 레버 / 레버 - 출구 경로의 최단경로 더하기
14+
maps = maps.map((row, x) => {
15+
row = row.split("")
16+
row.forEach((item, y) => {
17+
if (item === "S") S = [x, y]
18+
if (item === "L") L = [x, y]
19+
if (item === "E") E = [x, y]
20+
})
21+
return row
22+
})
23+
24+
const bfs = (start, end) => {
25+
const visited = Array.from({ length: n }, () => Array(m).fill(false))
26+
const q = [[...start, 0]]
27+
28+
while (q.length) {
29+
let [x, y, dist] = q.shift()
30+
if (x === end[0] && y === end[1]) return dist
31+
32+
for (let [dx, dy] of dir) {
33+
let [nx, ny] = [x + dx, y + dy]
34+
if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
35+
if (!visited[nx][ny] && maps[nx][ny] !== "X") {
36+
visited[nx][ny] = true
37+
q.push([nx, ny, dist + 1])
38+
}
39+
}
40+
}
41+
}
42+
43+
return -1
44+
}
45+
46+
let toL = bfs(S, L)
47+
if (toL === -1) return -1
48+
let toE = bfs(L, E)
49+
if (toE === -1) return -1
50+
51+
return toL + toE
52+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// n개의 송전탑이 트리 형태
2+
// 하나를 끊어서 2개로 분할하고자 함. 최대한 갯수 같게.
3+
// 와이어중에 하나 끊은다음에, 하나에 대해 bfs수행하기
4+
const solution = (n, wires) => {
5+
let diff = n
6+
const graph = Array.from({ length: n + 1 }, () => [])
7+
for ([a, b] of wires) {
8+
graph[a].push(b)
9+
graph[b].push(a)
10+
}
11+
// 송전선 별로 끊어보기(완전탐색)
12+
for ([a, b] of wires) {
13+
const visited = new Array(n + 1).fill(false)
14+
visited[b] = true // b로는 못가도록 처리
15+
16+
let cnt = 0
17+
const q = [a]
18+
visited[a] = true
19+
20+
while (q.length) {
21+
let cur = q.shift()
22+
cnt++
23+
24+
for (let next of graph[cur]) {
25+
if (!visited[next]) {
26+
visited[next] = true
27+
q.push(next)
28+
}
29+
}
30+
}
31+
32+
diff = Math.min(diff, Math.abs(cnt - (n - cnt))) // 전력망 노드 차이 갱신
33+
}
34+
return diff
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const solution = (maps) => {
2+
const n = maps.length
3+
const m = maps[0].length
4+
const dir = [
5+
[1, 0],
6+
[-1, 0],
7+
[0, -1],
8+
[0, 1],
9+
]
10+
const visited = Array.from({ length: n }, () => Array(m).fill(false))
11+
visited[0][0] = true
12+
const q = [[0, 0, 1]] // [x,y,거리] 저장
13+
14+
while (q.length) {
15+
let [x, y, depth] = q.shift()
16+
if (x === n - 1 && y === m - 1) {
17+
return depth
18+
}
19+
for (let [dx, dy] of dir) {
20+
let nx = x + dx
21+
let ny = y + dy
22+
if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
23+
if (maps[nx][ny] === 1 && !visited[nx][ny]) {
24+
visited[nx][ny] = true
25+
q.push([nx, ny, depth + 1])
26+
}
27+
}
28+
}
29+
}
30+
31+
return -1
32+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// DFS 풀이
2+
function solution(numbers, target) {
3+
let result = 0
4+
5+
const dfs = (acc, depth) => {
6+
if (depth === numbers.length) {
7+
if (acc === target) result++
8+
return
9+
}
10+
dfs(acc + numbers[depth], depth + 1)
11+
dfs(acc - numbers[depth], depth + 1)
12+
}
13+
14+
dfs(0, 0)
15+
return result
16+
}
17+
18+
// BFS 풀이
19+
function solution(numbers, target) {
20+
let result = 0
21+
const queue = []
22+
23+
queue.push([0, 0]) // [합계, 처리할 인덱스]
24+
25+
while (queue.length) {
26+
const [acc, depth] = queue.shift()
27+
28+
if (depth === numbers.length) {
29+
if (acc === target) result++
30+
continue
31+
}
32+
33+
queue.push([acc + numbers[depth], depth + 1])
34+
queue.push([acc - numbers[depth], depth + 1])
35+
}
36+
37+
return result
38+
}

0 commit comments

Comments
 (0)