Skip to content

Commit

Permalink
add (programmers - level2 2문제): js 풀이
Browse files Browse the repository at this point in the history
  • Loading branch information
padosum committed Feb 17, 2023
1 parent 89659a1 commit dff3993
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
86 changes: 86 additions & 0 deletions programmers/level2/거리두기 확인하기.js
@@ -0,0 +1,86 @@
function solution(places) {
const answer = [];

places.forEach((place, idx) => {
let flag = false;
const pArr = place.map(p => p.split(""));
for (let i = 0; i < pArr.length; i++) {
for (let j = 0; j < pArr[0].length; j++) {
if (pArr[i][j] === "P") {
// 우측
if (j + 1 < pArr[0].length) {
const right = pArr[i][j + 1];
if (right === "P") {
flag = true;
}

if (right === "O") {
if (j + 2 < pArr[0].length) {
const r = pArr[i][j + 2];
if (r === "P") {
flag = true;
}
}

if (i + 1 < pArr.length) {
const rightBottom = pArr[i + 1][j + 1];
if (rightBottom === "P") {
flag = true;
}
}

if (i - 1 >= 0) {
const rightTop = pArr[i - 1][j + 1];
if (rightTop === "P") {
flag = true;
}
}
}
}

// 좌측
if (j - 1 >= 0) {
const left = pArr[i][j - 1];
if (left === "P") {
flag = true;
}

if (left === "O") {
if (i + 1 < pArr.length) {
const leftBottom = pArr[i + 1][j - 1];
if (leftBottom === "P") {
flag = true;
}
}
if (i - 1 >= 0) {
const leftTop = pArr[i - 1][j - 1];
if (leftTop === "P") {
flag = true;
}
}
}
}

// 하단
if (i + 1 < pArr.length) {
const bottom = pArr[i + 1][j];
if (bottom === "P") {
flag = true;
}

if (bottom === "O") {
if (i + 2 < pArr.length) {
const b = pArr[i + 2][j];
if (b === "P") {
flag = true;
}
}
}
}
}
}
}
answer.push(flag ? 0 : 1);
});
return answer;
}
16 changes: 16 additions & 0 deletions programmers/level2/하노이의 탑.js
@@ -0,0 +1,16 @@
const hanoi = (n, from, to, temp, result) => {
if (n === 1) {
result.push([from, to]);
return;
}

hanoi(n - 1, from, temp, to, result);
result.push([from, to]);
hanoi(n - 1, temp, to, from, result);
};

function solution(n) {
const answer = [];
hanoi(n, 1, 3, 2, answer);
return answer;
}

0 comments on commit dff3993

Please sign in to comment.