Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions yeonju/bfs/BOJ/13913.js
Original file line number Diff line number Diff line change
@@ -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();
37 changes: 37 additions & 0 deletions yeonju/bfs/BOJ/1697.js
Original file line number Diff line number Diff line change
@@ -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();
55 changes: 55 additions & 0 deletions yeonju/brute-force/BOJ/14500/14500.py
Original file line number Diff line number Diff line change
@@ -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()
48 changes: 48 additions & 0 deletions yeonju/brute-force/BOJ/14889/14889.py
Original file line number Diff line number Diff line change
@@ -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()
77 changes: 77 additions & 0 deletions yeonju/brute-force/BOJ/3085/3085.py
Original file line number Diff line number Diff line change
@@ -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()
32 changes: 32 additions & 0 deletions yeonju/brute-force/BOJ/9095/9095.py
Original file line number Diff line number Diff line change
@@ -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()