Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

# 20231219 doozuu (이주희) 문제 풀이 업로드 #138

Merged
merged 1 commit into from
Dec 21, 2023
Merged
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
54 changes: 54 additions & 0 deletions Doozuu/231219/boj_DFS와BFS.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : 'ex.txt';
const input = fs.readFileSync(filePath).toString().trim().split('\n');

const [N, M, V] = input.shift().split(' ').map(Number);
const numbers = input.map((el) => el.split(' ').map(Number));
const graph = Array.from({length: N + 1}, () => []);

numbers.map(([from, to]) => {
Doozuu marked this conversation as resolved.
Show resolved Hide resolved
graph[from].push(to);
graph[to].push(from);
});

graph.forEach((nodes) => nodes.sort((a, b) => a - b));

// DFS
function DFS(graph, startNode) {
const visited = [];
let needVisit = [];

needVisit.push(startNode);

while (needVisit.length !== 0) {
const node = needVisit.shift();
if (!visited.includes(node)) {
visited.push(node);
needVisit = [...graph[node], ...needVisit];
}
}
return visited;
}
Doozuu marked this conversation as resolved.
Show resolved Hide resolved

// BFS
function BFS(graph, startNode) {
const visited = [];
let needVisit = [];

needVisit.push(startNode);

while (needVisit.length !== 0) {
const node = needVisit.shift();
if (!visited.includes(node)) {
visited.push(node);
needVisit = [...needVisit, ...graph[node]];
}
}
return visited;
}

const dfs = DFS(graph, V);
const bfs = BFS(graph, V);

console.log(dfs.join(' '));
console.log(bfs.join(' '));
44 changes: 44 additions & 0 deletions Doozuu/231219/boj_단지번호붙이기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : 'ex.txt';
const input = fs.readFileSync(filePath).toString().trim().split('\n');

const N = Number(input.shift());
const board = input.map((el) => el.split('').map(Number));
const visited = Array.from({length: N}, () => Array(N).fill(false));
const answer = [];
const move = [
[0, 1],
[0, -1],
[1, 0],
[-1, 0],
];
let count = 0;

function DFS(i, j) {
if (i < 0 || j < 0 || i >= N || j >= N || visited[i][j] || !board[i][j]) {
return;
}

count++;
visited[i][j] = true;

for (const [dx, dy] of move) {
const newX = i + dx;
const newY = j + dy;
DFS(newX, newY);
}

return count;
}

for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
if (board[i][j] && !visited[i][j]) {
answer.push(DFS(i, j));
count = 0;
}
}
}

console.log(answer.length);
console.log(answer.sort((a, b) => a - b).join('\n'));
28 changes: 28 additions & 0 deletions Doozuu/231219/boj_풍선터뜨리기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 메모리 초과

const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : 'ex.txt';
const input = fs.readFileSync(filePath).toString().trim().split('\n');

const N = Number(input[0]);
const arr = input[1].split(' ').map(Number);
let ballonNum = Array.from({length: N}, (_, i) => i + 1);
let resultArr = [];
let idx = 0;

let K = arr.splice(idx, 1)[0];
resultArr.push(ballonNum.splice(idx, 1)[0]);

while (arr.length > 0) {
if (K > 0) {
idx = (idx + (K - 1)) % arr.length;
} else {
K = K * -1;
idx = arr.length - idx - 1;
idx = arr.length - ((idx + K) % arr.length) - 1;
}
K = arr.splice(idx, 1)[0];
resultArr.push(ballonNum.splice(idx, 1)[0]);
}

console.log(resultArr.join(' '));
28 changes: 28 additions & 0 deletions Doozuu/231219/pro_광물캐기.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
function solution(picks, minerals) {
var answer = 0;
let len = Math.ceil(minerals.length / 5);
let maxLen = picks.reduce((a, b) => a + b);
let arr = [];
if (maxLen === 0) return 0; // 곡괭이가 하나도 없는 경우
minerals = minerals.splice(0, maxLen * 5); // 더이상 사용할 곡괭이가 없을 경우를 위해
Doozuu marked this conversation as resolved.
Show resolved Hide resolved

for (let a = 0; a < len; a++) {
let obj = {d: 0, i: 0, s: 0};
minerals.splice(0, 5).map((v) => obj[v[0]]++); // 광물 개수 카운트
arr.push([
obj.d * 1 + obj.i * 1 + obj.s * 1, // 곡괭이별 피로도 계산
obj.d * 5 + obj.i * 1 + obj.s * 1,
obj.d * 25 + obj.i * 5 + obj.s * 1,
]);
}
// 피로도가 큰 순서로 내림차순 정렬하여 다이아 -> 철 -> 돌 순서대로 곡괭이 사용
arr
.sort((a, b) => b[2] - a[2])
.map((v) => {
if (picks[0] > 0) return picks[0]--, (answer += v[0]);
Doozuu marked this conversation as resolved.
Show resolved Hide resolved
if (picks[1] > 0) return picks[1]--, (answer += v[1]);
if (picks[2] > 0) return picks[2]--, (answer += v[2]);
});

return answer;
}
53 changes: 53 additions & 0 deletions Doozuu/231219/pro_기둥과보설치.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// 통과 x

function solution(n, build_frame) {
let answer = [];

function isIncludes(target) {
return answer.some((arr) => JSON.stringify(arr) === JSON.stringify(target));
}

build_frame.forEach(([x, y, type, action]) => {
let isValid = false;
if (type === 0) {
// 1. 바닥 위
if (y === 0) isValid = true;
// 2. 보의 한쪽 끝 위
if (isIncludes([x - 1, y, 1]) || isIncludes([x + 1, y, 1])) isValid = true;
// 3. 다른 기둥 위
if (isIncludes([x, y - 1, 0])) isValid = true;
}
if (type === 1) {
// 1. 한 쪽 기둥 위
if (isIncludes([x, y - 1, 0]) || isIncludes([x + 1, y - 1, 0])) isValid = true;
// 2. 두 쪽 모두 다른 보와 연결
if (isIncludes([x - 1, y, 1]) && isIncludes([x + 1, y, 1])) isValid = true;
}

if (isValid) {
if (action === 0) {
if (isIncludes([x - 1, y, type]) && isIncludes([x + 1, y, type])) {
return;
}
const idx = answer.findIndex(([i, j, t]) => i === x && j === y && t === type);
if (idx > -1) answer.splice(idx, 1);
}
Doozuu marked this conversation as resolved.
Show resolved Hide resolved
if (action === 1) {
answer.push([x, y, type]);
}
}
});

return answer.sort((a, b) => {
if (a[0] === b[0]) {
if (a[1] === b[1]) {
return a[2] - b[2];
}
return a[1] - b[1];
}
return a[0] - b[0];
});
}
// x, y, 구조물 종류(0 기둥, 1 보), 설치/삭제(0 삭제, 1 설치)
// 기둥 설치 : 바닥, 보의 한쪽 끝 위, 다른 기둥 위
// 보 설치 : 한 쪽 기둥 위, 두 쪽 다른 보와 연결