diff --git "a/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/G5-7576/7576.md" "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/G5-7576/7576.md" new file mode 100644 index 0000000..93ef2f1 --- /dev/null +++ "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/G5-7576/7576.md" @@ -0,0 +1,31 @@ +`25/02/25` + +## 7576: 토마토 + +백준 7576 + +## 풀이 + +```Plain text +어렵지만 빨간 토마토를 생각하면서 꾹 참고 풀었습니다. + +토마토가 들어있는 보드의 입력을 받고, 익은 토마토가 어디에 있는지 파악합니다. +초기에 익은 토마토가 있으면 x위치, y위치, 날짜를 큐에 저장합니다. +큐에서 하나씩 꺼내면서 큐에 있었던 토마토의 상하좌우를 살피고 +상하좌우, 범위 넘치지 않음 조건에 부합하는 토마토가 있다면 +위치와 날짜(현재 날짜 + 1)를 저장한 객체를 큐에 저장합니다. +해당 행위를 큐가 끝날 때까지 반복해 주면 됩니다. + +자바스크립트에서 시간을 줄이기 위해서, +큐 관련 연산을 좌에서 꺼내는 방식이 아닌 인덱스를 올려주는 방식을 사용했습니다. +``` + +## 해결 + +![alt text](image.png) + +```Plain text +시간 초과 관련해서 조금 고려할 게 있지만 +그래도 골드 치고 완전 어렵지는 않다고 생각합니다. +추천! +``` diff --git "a/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/G5-7576/image.png" "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/G5-7576/image.png" new file mode 100644 index 0000000..15ae2d1 Binary files /dev/null and "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/G5-7576/image.png" differ diff --git "a/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/G5-7576/input.txt" "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/G5-7576/input.txt" new file mode 100644 index 0000000..485a0e1 --- /dev/null +++ "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/G5-7576/input.txt" @@ -0,0 +1,5 @@ +6 4 +0 -1 0 0 0 0 +-1 0 0 0 0 0 +0 0 0 0 0 0 +0 0 0 0 0 1 \ No newline at end of file diff --git "a/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/G5-7576/solution.js" "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/G5-7576/solution.js" new file mode 100644 index 0000000..c40ab4f --- /dev/null +++ "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/G5-7576/solution.js" @@ -0,0 +1,52 @@ +const fs = require("fs"); +const filePath = process.platform === "linux" ? 0 : "input.txt"; +const input = fs.readFileSync(filePath, "utf8").toString().trim().split("\n"); + +const solution = () => { + const [M, N] = input[0].split(" ").map(Number); + const boards = []; + const queue = []; + let queueIndex = 0; + + for (let i = 0; i < N; i++) { + const row = input[i + 1].split(" ").map(Number); + boards.push(row); + + for (let j = 0; j < M; j++) { + if (row[j] === 1) { + queue.push({ x: j, y: i, days: 0 }); + } + } + } + + let max_days = 0; + + const dx = [0, 0, 1, -1]; + const dy = [1, -1, 0, 0]; + + while (queueIndex < queue.length) { + const { x, y, days } = queue[queueIndex++]; + max_days = Math.max(max_days, days); + + for (let i = 0; i < 4; i++) { + const nx = x + dx[i]; + const ny = y + dy[i]; + + if (nx >= 0 && nx < M && ny >= 0 && ny < N && boards[ny][nx] === 0) { + boards[ny][nx] = 1; + queue.push({ x: nx, y: ny, days: days + 1 }); + } + } + } + + for (let i = 0; i < N; i++) { + if (boards[i].includes(0)) { + console.log(-1); + return; + } + } + + console.log(max_days); +}; + +solution(); diff --git "a/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S1-1697/1697.md" "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S1-1697/1697.md" new file mode 100644 index 0000000..5f04cd2 --- /dev/null +++ "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S1-1697/1697.md" @@ -0,0 +1,39 @@ +`25/02/27` + +## 1697: 숨바꼭질 + +백준 1697 + +## 풀이 + +```Plain text +예전에 특정 값을 가지고 경우의 수를 따져서 +그래프를 만들고 +그래프에서 너비 우선 탐색을 통해 +답에 도달했던 문제가 있었는데 +딱 그런 문제 같습니다. + +이 문제는 시간, 메모리가 조금 엄격해서 +고민을 많이 했는데 +queue 사용시 shift 쓰지 않고 +인덱스 올리기 방법을 사용했구요 + +원래는 기하급수적으로 커다란 값이면 더블링 통해서 +최대한 접근 후 값을 찾고자 했었는데 +이게 반례가 있었나 봐요 +자꾸 틀려서 +상한선을 지정을 해주고 (타겟보다 2배이상크면 탐색X) +하던대로 BFS 해줬습니다. +이번 BFS는 접근 잘 한 것 같은데 +더블링 관련해서는 좀 아쉬웠어요~.. +``` + +## 해결 + +![alt text](image.png) + +```Plain text +ㅠㅠ... +한없이 초라해지네요 +그래도 화이팅 +``` diff --git "a/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S1-1697/image.png" "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S1-1697/image.png" new file mode 100644 index 0000000..8417e69 Binary files /dev/null and "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S1-1697/image.png" differ diff --git "a/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S1-1697/input.txt" "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S1-1697/input.txt" new file mode 100644 index 0000000..4741b7c --- /dev/null +++ "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S1-1697/input.txt" @@ -0,0 +1,3 @@ +5 17 + + diff --git "a/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S1-1697/solution.js" "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S1-1697/solution.js" new file mode 100644 index 0000000..21c21b0 --- /dev/null +++ "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S1-1697/solution.js" @@ -0,0 +1,40 @@ +const fs = require("fs"); +const filePath = process.platform === "linux" ? 0 : "input.txt"; +const input = fs.readFileSync(filePath, "utf8").toString().trim().split("\n"); + +const solution = () => { + const [N, target] = input[0].split(" ").map(Number); + let queueIndex = 0; + const visited = new Set(); + + if (N === target) { + console.log(0); + return; + } + + if (N > target) { + console.log(N - target); + return; + } + + let queue = [[N, 0]]; + visited.add(N); + + const maxLimit = target * 2; + + while (queue.length > queueIndex) { + const [num, time] = queue[queueIndex++]; + + for (const next of [num - 1, num + 1, num * 2]) { + if (next < 0 || next > maxLimit || visited.has(next)) continue; + if (next === target) { + console.log(time + 1); + return; + } + visited.add(next); + queue.push([next, time + 1]); + } + } +}; + +solution(); diff --git "a/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S2-2630/2630.md" "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S2-2630/2630.md" new file mode 100644 index 0000000..a30ddb3 --- /dev/null +++ "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S2-2630/2630.md" @@ -0,0 +1,44 @@ +`25/02/26` + +## 2630: 색종이 만들기 + +백준 2630 + +## 풀이 + +```Plain text +계속 잘라서 하얀색 색종이가 몇 개인지, 파란색 색종이가 몇 개인지 +파악하는 문제입니다. + +잘랐을 때 요소의 색이 모두 같은지 검사해야 할 것 같고, +같으면 더 자르지 않고 +다르면 더 자르게 되겠는데요 +자른 범위를 큐에 넣어 검사하면 되지 않을까 싶고 + +상하좌우라고 해야하나 방향 정보도 상수로 정리하면 될 듯 하구요 + +길이가 1이 아닐 때까지 총길이 / 2 floor값으로 상하좌우를 나눠 주면 +될 것 같네요 + +전체적인 실행 순서는, + +1. 입력을 받고 보드를 만들어준다 +2. 보드내부 요소들이 모두 같은지 검사하는 함수 정의 +3. 보드를 나누어 4등분된 보드를 리턴하는 함수 정의 +4. 보드를 검사하는 절차가 있는 함수 실행 +5. 보드가 같은가? -> 보드의 첫번째 칸의 숫자에 따라 카운트 저장 +6. 보드 길이가 1인가? -> 마찬가지로 숫자에 따라 카운트 저장 +7. 위 두가지가 아니면 보드를 나누고, 나눈값을 대상으로 +8. 계속 보드 검사 함수 실행해서 해당 부분의 최종 카운트를 받아온다. +9. 최종적으로는 1/4파트에 대해 계속 나누어 검사 후 2/4 파트에 대해 검사, ... 하여 4/4 파트까지 모든 작업을 수행 후 결과를 반환한다. +``` + +## 해결 + +![alt text](image.png) + +```Plain text +이건 중간에 나눈 값들을 어떻게 처리하고 재귀 처리를 어떻게 할지에 대해 +검색을 좀 했어서 한번에 맞게 되었습니다.... +BFS 조금 더 풀면서 실력 디벨롭 해야겠습니다... +``` diff --git "a/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S2-2630/image.png" "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S2-2630/image.png" new file mode 100644 index 0000000..bca514a Binary files /dev/null and "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S2-2630/image.png" differ diff --git "a/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S2-2630/input.txt" "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S2-2630/input.txt" new file mode 100644 index 0000000..ab3d448 --- /dev/null +++ "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S2-2630/input.txt" @@ -0,0 +1,9 @@ +8 +1 1 0 0 0 0 1 1 +1 1 0 0 0 0 1 1 +0 0 0 0 1 1 0 0 +0 0 0 0 1 1 0 0 +1 0 0 0 1 1 1 1 +0 1 0 0 1 1 1 1 +0 0 1 1 1 1 1 1 +0 0 1 1 1 1 1 1 \ No newline at end of file diff --git "a/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S2-2630/solution.js" "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S2-2630/solution.js" new file mode 100644 index 0000000..e47f323 --- /dev/null +++ "b/\355\225\234\354\234\240\354\247\204/9\354\243\274\354\260\250/S2-2630/solution.js" @@ -0,0 +1,80 @@ +const fs = require("fs"); +const filePath = process.platform === "linux" ? 0 : "input.txt"; +const input = fs.readFileSync(filePath, "utf8").toString().trim().split("\n"); + +const solution = () => { + const M = Number(input[0]); + const board = []; + + for (let i = 0; i < M; i++) { + const row = input[i + 1].split(" ").map(Number); + board.push(row); + } + + function isSame(board) { + const firstValue = board[0][0]; + for (let i = 0; i < board.length; i++) { + for (let j = 0; j < board[i].length; j++) { + if (board[i][j] !== firstValue) { + return false; + } + } + } + return true; + } + + function divideBoard(board) { + const n = board.length; + const halfIndex = Math.floor(n / 2); + const newBoards = Array(4) + .fill() + .map(() => []); + + for (let i = 0; i < halfIndex; i++) { + newBoards[0].push(board[i].slice(0, halfIndex)); + } + + for (let i = 0; i < halfIndex; i++) { + newBoards[1].push(board[i].slice(halfIndex, n)); + } + + for (let i = halfIndex; i < n; i++) { + newBoards[2].push(board[i].slice(0, halfIndex)); + } + + for (let i = halfIndex; i < n; i++) { + newBoards[3].push(board[i].slice(halfIndex, n)); + } + + return newBoards; + } + + function processBoard(board) { + if (isSame(board)) { + return board[0][0] === 0 ? [1, 0] : [0, 1]; + } + + if (board.length === 1) { + return board[0][0] === 0 ? [1, 0] : [0, 1]; + // 길이 1이면 [0][0]의 숫자가 결국 해당 숫자의 카운트 up + } + + const quadrants = divideBoard(board); + let count0 = 0; + let count1 = 0; + + for (let quadrant of quadrants) { + const [q0, q1] = processBoard(quadrant); + count0 += q0; + count1 += q1; + } + + return [count0, count1]; + } + + const result = processBoard(board); + console.log(result[0]); + console.log(result[1]); +}; + +solution();