Baekjoon

Baekjoon 1913번 달팽이

ppwag 2022. 3. 29. 20:06

문제

https://www.acmicpc.net/problem/1913

걸린 시간

-

풀이

TypeScript

const fs = require('fs');
const filePath = process.platform === 'linux' ? '/dev/stdin' : './input';
const stdin = fs.readFileSync(filePath).toString().trim().split('\n').map((s: string) => s.trim());
const input = (() => {
  let line = 0;
  return () => stdin[line++];
})();

const n: number = parseInt(input());
const N: number = parseInt(input()); // 찾고자 하는 수

const snail: Array<Array<number>> = Array.from(Array(n), () => Array(n).fill(0));

const sy: number = Math.floor(n/2);
const sx: number = Math.floor(n/2);

snail[sy][sx] = 1;

let y: number = sy;
let x: number = sx;
let move: number = 1;
let digit = 1;
let round = 0;
const dy: Array<number> = [-1, 0, 1, 0]; // 시계 방향
const dx: Array<number> = [0, 1, 0, -1];
let fy: number = sy;
let fx: number = sx;

while(digit < n*n){
  for(let i = 0; i < move && digit < n*n; i++){
    y += dy[round%4];
    x += dx[round%4];
    digit++;
    snail[y][x] = digit;
    if(digit === N){
      fy = y;
      fx = x;
    }
  }
  if(round%2 === 1) move++;
  round++;
}

for(let i = 0; i < n; i++){
  console.log(snail[i].join(' '));
}
console.log(fy+1, fx+1);

100% 에서 틀렸습니다 가 출력되었는데 원인은 확인하려는 좌표가 숫자 1일 때의 처리를 해 주지 않아서였다.

'Baekjoon' 카테고리의 다른 글

Baekjoon 2567번 색종이 - 2  (0) 2022.04.08
Baekjoon 2659번 십자카드 문제  (0) 2022.04.03
Baekjoon 1904번 01타일  (0) 2022.03.16
Baekjoon 10799번 쇠막대기  (0) 2022.03.14
Baekjoon 2480번 주사위 세개  (0) 2022.03.03

댓글