게임 캐릭터를 4가지 명령어를 통해 움직이려 합니다. 명령어는 다음과 같습니다.
U: 위쪽으로 한 칸 가기
D: 아래쪽으로 한 칸 가기
R: 오른쪽으로 한 칸 가기
L: 왼쪽으로 한 칸 가기
캐릭터는 좌표평면의 (0, 0) 위치에서 시작합니다. 좌표평면의 경계는 왼쪽 위(-5, 5), 왼쪽 아래(-5, -5), 오른쪽 위(5, 5), 오른쪽 아래(5, -5)로 이루어져 있습니다.

예를 들어, "ULURRDLLU"로 명령했다면

1번 명령어부터 7번 명령어까지 다음과 같이 움직입니다.

8번 명령어부터 9번 명령어까지 다음과 같이 움직입니다.

이때, 우리는 게임 캐릭터가 지나간 길 중 캐릭터가 처음 걸어본 길의 길이를 구하려고 합니다. 예를 들어 위의 예시에서 게임 캐릭터가 움직인 길이는 9이지만, 캐릭터가 처음 걸어본 길의 길이는 7이 됩니다. (8, 9번 명령어에서 움직인 길은 2, 3번 명령어에서 이미 거쳐 간 길입니다)
단, 좌표평면의 경계를 넘어가는 명령어는 무시합니다.
예를 들어, "LULLLLLLU"로 명령했다면

1번 명령어부터 6번 명령어대로 움직인 후, 7, 8번 명령어는 무시합니다. 다시 9번 명령어대로 움직입니다.

이때 캐릭터가 처음 걸어본 길의 길이는 7이 됩니다.
명령어가 매개변수 dirs로 주어질 때, 게임 캐릭터가 처음 걸어본 길의 길이를 구하여 return 하는 solution 함수를 완성해 주세요.
dirs : "ULURRDLLU"
7
dirs는 string형으로 주어지며, 'U', 'D', 'R', 'L' 이외에 문자는 주어지지 않습니다.
dirs의 길이는 500 이하의 자연수입니다.
x좌표-y1좌표:x좌표-y2좌표와 x좌표-y2좌표:x좌표-y1좌표이다. visited에 추가할 때마다 answer를 1씩 증가시켜준다.function solution(dirs) {
var answer = 0;
let visited = new Set();
let current = [0, 0];
for(let i=0; i<dirs.length; i++) {
if(dirs[i]==="U") {
if(current[1]+1<=5) {
if(!visited.has(`${current[0]}-${current[1]}:${current[0]}-${current[1]+1}`)) {
visited.add(`${current[0]}-${current[1]}:${current[0]}-${current[1]+1}`);
visited.add(`${current[0]}-${current[1]+1}:${current[0]}-${current[1]}`);
answer++;
}
current[1]++;
}
} else if(dirs[i]==="D") {
if(current[1]-1>=-5) {
if(!visited.has(`${current[0]}-${current[1]}:${current[0]}-${current[1]-1}`)) {
visited.add(`${current[0]}-${current[1]}:${current[0]}-${current[1]-1}`);
visited.add(`${current[0]}-${current[1]-1}:${current[0]}-${current[1]}`);
answer++;
}
current[1]--;
}
} else if(dirs[i]==="R") {
if(current[0]+1<=5) {
if(!visited.has(`${current[0]+1}-${current[1]}:${current[0]}-${current[1]}`)) {
visited.add(`${current[0]+1}-${current[1]}:${current[0]}-${current[1]}`);
visited.add(`${current[0]}-${current[1]}:${current[0]+1}-${current[1]}`);
answer++;
}
current[0]++;
}
} else {
if(current[0]-1>=-5) {
if(!visited.has(`${current[0]}-${current[1]}:${current[0]-1}-${current[1]}`)) {
visited.add(`${current[0]}-${current[1]}:${current[0]-1}-${current[1]}`);
visited.add(`${current[0]-1}-${current[1]}:${current[0]}-${current[1]}`);
answer++;
}
current[0]--;
}
}
}
return answer;
}
문제 구현 자체는 어렵지 않긴 하지만, 처음 걸어본 길만을 구하는 문제는 조금 구현이 쉽게 떠오르는 정도는 아니여서 기록하는 문제. Set 저장 코드가 굉장히 안 예쁘게 나왔다..