From b4ca515c251aed19b60c5b4130b9398fa00df53d Mon Sep 17 00:00:00 2001 From: Yeonju Jo Date: Wed, 1 Nov 2023 16:45:22 +0900 Subject: [PATCH 1/6] add: BOJ 3085 --- yeonju/brute-force/BOJ/3085/3085.py | 77 +++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 yeonju/brute-force/BOJ/3085/3085.py diff --git a/yeonju/brute-force/BOJ/3085/3085.py b/yeonju/brute-force/BOJ/3085/3085.py new file mode 100644 index 0000000..d5b3780 --- /dev/null +++ b/yeonju/brute-force/BOJ/3085/3085.py @@ -0,0 +1,77 @@ +import sys + +input = sys.stdin.readline + + +def read_string_list(): + return list(input().rstrip()) + + +def read_int(): + return int(input()) + + +def check(arr, n): + result = 1 + + for i in range(n): + colCnt = 1 + for j in range(1, n): + if arr[i][j] == arr[i][j-1]: + colCnt += 1 + else: + colCnt = 1 + + if colCnt > result: + result = colCnt + + rowCnt = 1 + for j in range(1, n): + if arr[j][i] == arr[j-1][i]: + rowCnt += 1 + else: + rowCnt = 1 + + if rowCnt > result: + result = rowCnt + + return result + + +def main(): + n = read_int() + candy = [] + result = 0 + + for _ in range(n): + candy.append(read_string_list()) + + for i in range(n): + for j in range(n): + # 열 바꾸기 + if j+1 < n: + candy[i][j], candy[i][j+1] = candy[i][j+1], candy[i][j] + + tmp = check(candy, n) + + if tmp > result: + result = tmp + + candy[i][j], candy[i][j+1] = candy[i][j+1], candy[i][j] + + # 행 바꾸기 + if i+1 < n: + candy[i][j], candy[i+1][j] = candy[i+1][j], candy[i][j] + + tmp = check(candy, n) + + if tmp > result: + result = tmp + + candy[i][j], candy[i+1][j] = candy[i+1][j], candy[i][j] + + print(result) + + +if __name__ == "__main__": + main() From 63ad962612cc32ac41a2661bb5070f756e580b6d Mon Sep 17 00:00:00 2001 From: Yeonju Jo Date: Wed, 1 Nov 2023 20:22:14 +0900 Subject: [PATCH 2/6] add: BOJ 14500 --- yeonju/brute-force/BOJ/14500/14500.py | 55 +++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 yeonju/brute-force/BOJ/14500/14500.py diff --git a/yeonju/brute-force/BOJ/14500/14500.py b/yeonju/brute-force/BOJ/14500/14500.py new file mode 100644 index 0000000..2f8c43e --- /dev/null +++ b/yeonju/brute-force/BOJ/14500/14500.py @@ -0,0 +1,55 @@ +import sys + +INF = float('inf') + +input = sys.stdin.readline + + +def read_int_list(): + return list(map(int, input().split())) + + +def main(): + n, m = read_int_list() + tetromino = [read_int_list() for _ in range(n)] + + visited = [([0] * m) for _ in range(n)] + result = 0 + max_arr = max(map(max, tetromino)) + + def dfs(x, y, idx, total): + nonlocal result + dx = [-1, 0, 1, 0] + dy = [0, 1, 0, -1] + + if result >= total + max_arr * (3 - idx): + return + + if idx == 3: + result = max(result, total) + return + + for i in range(4): + nx = x + dx[i] + ny = y + dy[i] + if 0 <= nx < n and 0 <= ny < m and visited[nx][ny] == 0: + if idx == 1: + visited[nx][ny] = 1 + dfs(x, y, idx + 1, total + tetromino[nx][ny]) + visited[nx][ny] = 0 + + visited[nx][ny] = 1 + dfs(nx, ny, idx + 1, total + tetromino[nx][ny]) + visited[nx][ny] = 0 + + for x in range(n): + for y in range(m): + visited[x][y] = 1 + dfs(x, y, 0, tetromino[x][y]) + visited[x][y] = 0 + + print(result) + + +if __name__ == "__main__": + main() From bd4dc7d5f765e815675d2be82b6ab384bed51a20 Mon Sep 17 00:00:00 2001 From: Yeonju Jo Date: Wed, 1 Nov 2023 23:11:54 +0900 Subject: [PATCH 3/6] add: BOJ 9095 --- yeonju/brute-force/BOJ/9095/9095.py | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 yeonju/brute-force/BOJ/9095/9095.py diff --git a/yeonju/brute-force/BOJ/9095/9095.py b/yeonju/brute-force/BOJ/9095/9095.py new file mode 100644 index 0000000..47677d3 --- /dev/null +++ b/yeonju/brute-force/BOJ/9095/9095.py @@ -0,0 +1,32 @@ +import sys + +input = sys.stdin.readline + + +def read_int(): + return int(input()) + + +def main(): + t = read_int() + result = [] + + for _ in range(t): + n = read_int() + dp = [0] * 12 + + dp[1] = 1 + dp[2] = 2 + dp[3] = 4 + + for i in range(4, n + 1): + dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3] + + result.append(dp[n]) + + for i in range(t): + print(result[i]) + + +if __name__ == "__main__": + main() From a7cda710df28a477be7cd6a1df2e3c7983f1bfbf Mon Sep 17 00:00:00 2001 From: Yeonju Jo Date: Thu, 2 Nov 2023 15:11:54 +0900 Subject: [PATCH 4/6] add: BOJ 14889 --- yeonju/brute-force/BOJ/14889/14889.py | 48 +++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 yeonju/brute-force/BOJ/14889/14889.py diff --git a/yeonju/brute-force/BOJ/14889/14889.py b/yeonju/brute-force/BOJ/14889/14889.py new file mode 100644 index 0000000..468ae87 --- /dev/null +++ b/yeonju/brute-force/BOJ/14889/14889.py @@ -0,0 +1,48 @@ +import sys + +INF = float('inf') + +input = sys.stdin.readline + + +def read_int_list(): + return list(map(int, input().split())) + + +def read_int(): + return int(input()) + + +def main(): + n = read_int() + teams = [read_int_list() for _ in range(n)] + visited = [False for _ in range(n)] + min_diff = INF + + def back(depth, idx): + nonlocal min_diff + + if depth == n // 2: # 팀이 다 나눠졌으면 + start, link = 0, 0 + + for i in range(n): + for j in range(n): + if visited[i] and visited[j]: # start 팀 + start += teams[i][j] + elif not visited[i] and not visited[j]: # link팀 + link += teams[i][j] + + min_diff = min(min_diff, abs(start - link)) + + for i in range(idx, n): + if not visited[i]: + visited[i] = True + back(depth + 1, i + 1) + visited[i] = False + + back(0, 0) + print(min_diff) + + +if __name__ == "__main__": + main() From 0b7b86ed3fc4d4c9d761f48af2dac6a8552576b1 Mon Sep 17 00:00:00 2001 From: Yeonju Jo Date: Wed, 29 Nov 2023 01:08:33 +0900 Subject: [PATCH 5/6] add: BOJ 1697 --- yeonju/bfs/BOJ/1697.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 yeonju/bfs/BOJ/1697.js diff --git a/yeonju/bfs/BOJ/1697.js b/yeonju/bfs/BOJ/1697.js new file mode 100644 index 0000000..42eb6e2 --- /dev/null +++ b/yeonju/bfs/BOJ/1697.js @@ -0,0 +1,37 @@ +/** + * 1트: 모르겠어서 정답 확인,, + * 풀이방법: bfs + */ + +function solution() { + const fs = require("fs"); + const input = fs.readFileSync("example.txt").toString().trim().split(" "); + + const [n, k] = input.map(Number); + + let queue = [[n, 0]]; + let visited = new Array(100001).fill(false); + + while (queue.length) { + const [cur, depth] = queue.shift(); + + if (visited[cur]) { + continue; + } + + visited[cur] = true; + + if (cur === k) { + console.log(depth); + break; + } + + for (let next of [cur + 1, cur - 1, cur * 2]) { + if (!visited[next] && next >= 0 && next <= 100000) { + queue.push([next, depth + 1]); + } + } + } +} + +solution(); From 92f5036030069c5c910abfad72ec8dee00a75780 Mon Sep 17 00:00:00 2001 From: Yeonju Jo Date: Wed, 29 Nov 2023 20:47:42 +0900 Subject: [PATCH 6/6] add: BOJ 13913 --- yeonju/bfs/BOJ/13913.js | 110 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 yeonju/bfs/BOJ/13913.js diff --git a/yeonju/bfs/BOJ/13913.js b/yeonju/bfs/BOJ/13913.js new file mode 100644 index 0000000..973f9a7 --- /dev/null +++ b/yeonju/bfs/BOJ/13913.js @@ -0,0 +1,110 @@ +/** + * 1트: dfs -> 메모리 초과 + * 깊이 우선 탐색은 너무 많이 탐색,, 최단 경로는 bfs가 적합 + * + * 반례 # 1 -> Maximum call stack size exceeded + * 입력: 24 300 + * 출력: 10 + * 24 23 22 21 20 19 38 76 75 150 300 + * + * 반례 # 2 -> 시간 초과 + * 입력: 2 1024 + * 출력: 9 + * 2 4 8 16 32 64 128 256 512 1024 + * + * 반례 # 3 -> Maximum call stack size exceeded + * 입력: 100 1 + * 출력: 99 + * 100~1까지 순서대로 + */ +function solution1() { + const fs = require("fs"); + const input = fs.readFileSync("example.txt").toString().trim().split(" "); + + const [n, k] = input.map(Number); + + let visited = new Array(100_001).fill(false); + let answer = Number.MAX_SAFE_INTEGER; + let answer_path = []; + + function dfs(path, depth, visited) { + console.log(path); + if (depth > answer) { + return; + } + + let cur = path[path.length - 1]; + + if (cur === k) { + if (depth < answer) { + answer = depth; + answer_path = [...path]; + } + return; + } + + for (let next of [cur + 1, cur - 1, cur * 2]) { + if (!visited[next] && next >= 0 && next <= 100_000) { + visited[next] = true; + dfs([...path, next], depth + 1, visited); + visited[next] = false; + } + } + } + + dfs([n], 0, visited); + console.log(answer); + console.log(...answer_path); +} + +/** + * 2트: bfs + * 최단경로이기 때문에 bfs로 빠르게 탐색할 수 있음 + */ +function solution2() { + const fs = require("fs"); + const input = fs.readFileSync("example.txt").toString().trim().split(" "); + + const [n, k] = input.map(Number); + + let visited = new Array(100_001).fill(false); + let path = new Array(100_001).fill(0); + + function bfs(n) { + let queue = [[n, 0]]; + visited[n] = 1; + + while (queue.length) { + console.log(path.slice(0, 20)); + const [cur, depth] = queue.shift(); + + if (cur === k) { + return depth; + } + + for (let next of [cur - 1, cur + 1, cur * 2]) { + if (!visited[next] && next >= 0 && next <= 100_000) { + path[next] = cur; + visited[next] = true; + queue.push([next, depth + 1]); + } + } + } + } + + const times = bfs(n); + + let order = []; + order.push(k); + + let prev = path[k]; + for (let i = 0; i < times; i++) { + order.push(prev); + prev = path[prev]; + } + + console.log(times); + console.log(...order.reverse()); +} + +solution2();