Skip to content

Commit

Permalink
#21 22.07.15 > 구현 연습 2문제
Browse files Browse the repository at this point in the history
  • Loading branch information
beurmuz committed Jul 15, 2022
1 parent d86db8e commit b235e05
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/cote/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# COTE
||문제종류|문제명|응시 날짜|채점 결과|재응시예정|
|:-:|:-|:-|:---:|:---:|:-:|
|1|[Greedy](./greedy/)|[예제](./greedy/greedy_ex.js) <br> [연습1](./greedy/greedy_01.js) <br> [연습2](./greedy/greedy_02.js) <br> [연습3](./greedy/greedy_03.js)|22.07.14| O <br> X <br> O <br> O |X <br> O <br> X <br> X|
|1|[Greedy](./greedy/)|[예제](./greedy/greedy_ex.js)<br>[연습1](./greedy/greedy_01.js)<br>[연습2](./greedy/greedy_02.js)<br>[연습3](./greedy/greedy_03.js)|22.07.14|O<br>X<br>O<br>O|X<br>O<br>X<br>X|
|2|[Implementation](./implementation/)|[예제1](./implementation/imple_ex01.js)<br>[예제2](./implementation/imple_ex02.js)<br>[연습1](./implementation/imple_01.js)<br>[연습2](./implementation/imple_02.js)|22.07.15|△<br>△<br>△<br>X|O<br>O<br>O<br>O|
18 changes: 18 additions & 0 deletions src/cote/implementation/imple_01.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

function solution(n) {
let answer = 0;
const colList = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
const col = colList.indexOf(n[0])+1;
const row = n[1]*1;
const steps = [[-2, 1], [-1, 2], [1, 2], [2, 1], [2,-1], [1,-2], [-1, -2], [-2, -1]];

steps.forEach((step) => {
let nx = row + step[0];
let ny = col + step[1];
if(nx >= 1 && ny >= 1 && nx <= 8 && ny <= 8) answer++;
});
return answer;
}

console.log(solution('a1'));
64 changes: 64 additions & 0 deletions src/cote/implementation/imple_02.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'use strict';

function solution(N, M, x, y, d, map) {
// 방문한 위치를 저장할 map
const visitedMap = Array.from(Array(N), () => Array(M).fill(0));

visitedMap[x][y] = 1; // 방문 처리

// 북, 동, 남, 서
const dx = [-1, 0, 1, 0];
const dy = [0, -1, 0, 1];

let direction = 0;

// 왼쪽으로 회전
function turnLeft() {
direction -= 1;
if(direction == -1) direction = 3;
}

// 시뮬레이션
let count = 1;
let turn_time = 0;

while(1) {
turnLeft();
let nx = x + dx[direction];
let ny = y + dy[direction];

// 회전 후 정면에 가보지 않은 칸이 존재하는 경우 이동함
if(visitedMap[nx][ny] === 0 && map[nx][ny] === 0) {
visitedMap[nx][ny] = 1; // 방문 표시
x = nx; // 현재 위치 저장
y = ny; // 현재 위치 저장
count++;
turn_time = 0;
continue;
} else {
turn_time += 1;
}

// 네 방향 모두 갈 수 없는 경우
if(turn_time === 4) {
nx = x - dx[direction];
ny = y - dy[direction];

// 뒤로 갈 수 있다면 이동하기
if (map[nx][ny] === 0) {
x = nx;
y = ny;
} else {
break;
}
turn_time = 0;
}
}
return count;
}

let map = [[1, 1, 1, 1],
[1, 0, 0, 1],
[1, 1, 0, 1],
[1, 1, 1, 1]];
console.log(solution(4, 4, 1, 1, 0, map));

0 comments on commit b235e05

Please sign in to comment.