Skip to content

Commit 7bf4ec7

Browse files
authored
🌱: printer.js를 수정
Github에서 수정한 커밋입니다.
1 parent d90ba00 commit 7bf4ec7

File tree

1 file changed

+1
-59
lines changed

1 file changed

+1
-59
lines changed

javascript/source/printer.js

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,4 @@
1-
let path = require('path');
2-
let inputPath = path.join(__dirname, '/dev/stdin'); // __dirname은 현재 스크립트의 디렉토리 절대경로
31
let input = require('fs').readFileSync(inputPath).toString().trim().split('\r\n');
42
//let input = require('fs').readFileSync('/dev/stdin').toString().trim().split('\n'); //백준 제출
53

6-
// 왜 맵으로 문제를 풀 수 없는가
7-
// 첫 번째 입력값을 추출하고 나머지를 2차원 배열로 변환
8-
const N = +input.shift(); //3
9-
const arr = input.map(el => el.split(' ').map(el => +el)); //.map(Number)
10-
11-
console.log(arr);
12-
13-
const dfs = () => {
14-
//2차원 배열 생성하는 구문
15-
//Array.from -> Array.from(arrayLike, mapFn)
16-
const isVisited = Array.from({ length: N }, () => Array(N).fill(false)); // 방문 기록 배열
17-
//new array 방식으로는 불가한가?
18-
const currentPos = [[0, 0]]; //DFS를 탐색을 시작할 좌표를 담는 초기 스택
19-
//왜 currentPos은 2차원 배열이여야 하는가?
20-
let answer = false;
21-
22-
//왜 [x,y]가 아닌 y,x인가?
23-
/**
24-
* const [y, x] = currentPos.pop()에서 y와 x는 순서대로 y축(세로)과 x축(가로)의 좌표를 의미합니다. 좌표를 다룰 때 보통 (y, x) 형태로 사용합니다. 이 구조는 2차원 배열에서 먼저 행(row)인 y가 나오고, 그다음에 열(column)인 x가 나오는 방식이다.
25-
*/
26-
while (currentPos.length > 0) {
27-
const [y, x] = currentPos.pop(); //currentPos의 마지막 요소 pop -> 구조 분해 할당으로 pop이 반환한 값을 받는다.
28-
console.log(`현재 위치: [${y}, ${x}]`);
29-
const currentMov = arr[y][x]; //해당 x,y위치 값
30-
//currentMov 가 움직이는 칸의 갯수
31-
//단, y,x 의 위 이동 기준은
32-
console.log(`현재 값: ${currentMov}`);
33-
34-
if (currentMov === -1) { // 종료 조건
35-
console.log('목표에 도착');
36-
answer = true;
37-
break;
38-
}
39-
40-
// y축 방향으로 이동
41-
if (y + currentMov < N && !isVisited[y + currentMov][x]) {
42-
//isVisited[y + currentMov][x] 부분이 true인지 false인지 확인하는 조건문이다.
43-
currentPos.push([y + currentMov, x]);
44-
isVisited[y + currentMov][x] = true;
45-
console.log(`y축 이동: [${y + currentMov}, ${x}]`);
46-
}
47-
48-
// x축 방향으로 이동
49-
if (x + currentMov < N && !isVisited[y][x + currentMov]) {
50-
currentPos.push([y, x + currentMov]);
51-
isVisited[y][x + currentMov] = true;
52-
console.log(`x축 이동: [${y}, ${x + currentMov}]`);
53-
}
54-
console.log('스택 상태:', currentPos);
55-
console.log('방문 기록:', isVisited);
56-
}
57-
58-
// 결과 출력
59-
console.log(answer ? 'HaruHaru' : 'Hing');
60-
};
61-
62-
dfs();
4+
console.log('Hello, World!');

0 commit comments

Comments
 (0)