방문 길이

유태형·2022년 2월 13일
0

문제

문제 분석

처음 문제를 풀면서 -5~5 사이 좌표값을 가지면 되겠다 생각을 했었다. 그래서 5와 -5를 벗어나는 이동은 계산하지 않으면서 풀었었다.




풀이

좌표

-5와 5사이의 X,Y 좌표를 가져야 한다. 따라서 넘어가는 경로는 고려되지 않아야 함

		if(nextX > 5 || nextX < -5 || nextY > 5 || nextY < -5)
           continue;

방향

중복된 경로를 어떻게 연산하지 않을지는 가장 힘든 부분이었고 여러 블로그를 찾아보기도 하였다. 그리고 U 와 D L 과 R을 연산할때 서로 서로 반대 방향으로 저장하면 같은 경로 다른 방향에서도 같은 경로로 취급하기 때문에 옳바른 값을 도출 해 낼 수 있었다.

			case 'U':
               nextY++;
               road += nowX;
               road += nowY;
               road += nextX;
               road += nextY;
               break;
           case 'D':
               nextY--;
               road += nextX;
               road += nextY;
               road += nowX;
               road += nowY;
               break;
           case 'R':
               nextX++;
               road += nowX;
               road += nowY;
               road += nextX;
               road += nextY;
               break;
           case 'L':
               nextX--;
               road += nextX;
               road += nextY;
               road += nowX;
               road += nowY;
               break;

U와 R은 현재 좌표부터 D와 L은 다음 좌표부터 저장함으로써 반대방향으로 같은 경로를 구분할 수 있다.




코드

import java.util.*;

class Solution {
    public int solution(String dirs) {
        //Set : 중복 허용 안함 -> 경로 중복 제거
        Set<String> s = new HashSet<String>();
        
        int nowX = 0;
        int nowY = 0;
        int nextX;
        int nextY;
        // U와R는 now -> next로 이동
        // D와L는 next -> now로 이동
        // 상반되는 방향성을 반영
        for(int i = 0; i < dirs.length(); i++){
            nextX = nowX;
            nextY = nowY;
            String road = "";
            
            switch(dirs.charAt(i)){
                case 'U': //nowX nowY nextX nextY
                    nextY++;
                    road += nowX;
                    road += nowY;
                    road += nextX;
                    road += nextY;
                    break;
                case 'D': //nextX nextY nowX nowY
                    nextY--;
                    road += nextX;
                    road += nextY;
                    road += nowX;
                    road += nowY;
                    break;
                case 'R': //nowX nowY nextX nextY
                    nextX++;
                    road += nowX;
                    road += nowY;
                    road += nextX;
                    road += nextY;
                    break;
                case 'L': //nextX nextY nowX nowY
                    nextX--;
                    road += nextX;
                    road += nextY;
                    road += nowX;
                    road += nowY;
                    break;
            }
            
            // -5 ~ 5사이에서만 이동 가능
            if(nextX > 5 || nextX < -5 || nextY > 5 || nextY < -5)
                continue;
            
            //현재 이동 경로 저장
            s.add(road);
            //다음 위치로
            nowX = nextX;
            nowY = nextY;
        }
        //Set에는 중복 경로가 없음
        return s.size();
    }
}



참고

https://excited-hyun.tistory.com/192




GitHub

https://github.com/ds02168/Study_Algorithm/blob/master/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/%EC%9E%90%EB%B0%94/Level2/%EA%B0%80%EC%9E%A5%ED%81%B0%EC%A0%95%EC%82%AC%EA%B0%81%ED%98%95%EC%B0%BE%EA%B8%B0.java

profile
오늘도 내일도 화이팅!

0개의 댓글

관련 채용 정보