백준 20436 / ZOAC 3 / JS

Jihoo·2021년 9월 12일
0

Algorithm

목록 보기
3/16

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

문제

자음은 왼손으로, 모음은 오른손으로 입력합니다.
이동하는 데에는 |x1-x2|+|y1-y2| 만큼, 키를 누르는 데에는 1만큼의 시간이 걸립니다.

구현

키를 누르는 시간은 최종적으로 입력 문자열(이하 target)의 길이만큼 추가되겠죠? 미리 더합니다.

let answer = target.length;

이후 target을 돌면서 각 문자의 위치(이하 targetLocation)를 받아오고,
targetLocation이 자음칸에 위치하면 왼손과의 거리를 측정하고,
모음칸에 위치하면 오른손과의 거리를 측정해서 answer에 더합니다.

for (let i = 0; i < target.length; i++) {
  const targetLocation = getLocation(target[i]);
  if (
    targetLocation[1] < 5 &&
    !(targetLocation[0] === 2 && targetLocation[1] === 4)
  ) {
    answer += getDistance(left, targetLocation);
    left = targetLocation.slice();
  } else {
    answer += getDistance(right, targetLocation);
    right = targetLocation.slice();
  }
}

0개의 댓글