1. 문제
https://www.acmicpc.net/problem/1913
2. 풀이과정
일단 단순 규칙만 파악해서 풀었는데 방향을 바꿀때 로직을 단순화 시킬 수 있을 것 같아.
나중에 리펙토링 해보려고한다.
3. 자바스크립트 정답 코드
const fs = require('fs');
const filepath = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
const input = fs.readFileSync(filepath).toString().trim().split('\n');
const n = +input[0];
const target = +input[1];
let targetPoint = [];
let currentRow = Math.floor(n/2);
let currentCol = Math.floor(n/2);
let board = Array.from({length : n}, () => Array(n).fill(0));
let currentSize = 3;
let i = 1;
board[currentRow][currentCol] = i++;
checkPoint(currentRow, currentCol)
while(currentSize <= n){
currentRow -= 1; //위로 1칸
board[currentRow][currentCol] = i++;
checkPoint(currentRow, currentCol)
for(let j = 0; j< currentSize-2; j++){ //오른쪽으로
currentCol += 1;
board[currentRow][currentCol] = i++;
checkPoint(currentRow, currentCol)
}
for(let j = 0; j< currentSize-1; j++){ //아래로
currentRow += 1;
board[currentRow][currentCol] = i++;
checkPoint(currentRow, currentCol)
}
for(let j = 0; j< currentSize-1; j++){ //왼쪽으로
currentCol -= 1;
board[currentRow][currentCol] = i++;
checkPoint(currentRow, currentCol)
}
for(let j = 0; j< currentSize-1; j++){ //위로
currentRow -= 1;
board[currentRow][currentCol] = i++;
checkPoint(currentRow, currentCol)
}
currentSize += 2;
}
printBoard();
console.log(`${targetPoint[0]} ${targetPoint[1]}`);
function printBoard(){
for(let k = 0; k<n; k++){
console.log(board[k].join(" "));
}
}
function checkPoint(r, c){
if(board[r][c] != target)
return;
else
targetPoint =[r+1, c+1];
}
'🏆 PS(Problem Solving) > Baekjoon' 카테고리의 다른 글
[Node.js] 백준 Javascript 11501 주식 (0) | 2024.08.10 |
---|---|
[Node.js] 백준 Javascript 20291 파일 정리 (0) | 2024.06.07 |
[Node.js] 백준 Javascript 21608 상어 초등학교 (0) | 2024.06.04 |
[Node.js] 백준 Javascript 20436 ZOAC 3 (1) | 2024.06.03 |
[Node.js] 백준 Javascript 1244 스위치 켜고 끄기 (0) | 2024.06.01 |