Notice
Recent Posts
Recent Comments
Link
«   2025/07   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

Mini

[구현] 프로그래머스 방문길이 // set 본문

Algorithm/구현

[구현] 프로그래머스 방문길이 // set

Mini_96 2025. 7. 3. 14:13

https://school.programmers.co.kr/learn/courses/30/lessons/49994?gad_source=1&gad_campaignid=22199869887&gbraid=0AAAAAC_c4nB6yISFBx5mGvyOG08zAQ87t&gclid=CjwKCAjwsZPDBhBWEiwADuO6y9aUyXD7gRRtDHQNWI8nlF7PY_7YefwJekHo0m-V-AOfGdA5TBefwxoCEsgQAvD_BwE

 

프로그래머스

SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr

* 풀이

좌표를 (0,0)으로 변경

set에 from, to를 정렬후 넣기 (좌->우, 위->아래 순서)

set.size가 정답일것

import java.util.*;

class Pos{
    int y, x;
    Pos(int y, int x){
        this.y=y;
        this.x=x;
    }
}

class Solution {
    
    static void print(Object o){
       System.out.println(o);
    }
    
    public int solution(String dirs) {
        int answer = 0;
        int[][] arr = new int[11][11];
        // print(Arrays.deepToString(arr));
        
        Set<String> s = new HashSet<>();
        
        Pos cur = new Pos(5,5);
        for(int i=0;i<dirs.length();++i){
            var a = dirs.charAt(i);
            if(a=='U'){
                int nx = cur.x;
                int ny=cur.y-1;
                if(nx<0 || ny<0 || nx>=11 || ny>=11) continue;
                var sb = new StringBuilder();
                sb.append(ny);
                sb.append(nx);
                sb.append(cur.y);
                sb.append(cur.x);
                s.add(sb.toString());
                cur = new Pos(ny,nx);
            }
            else if(a=='D'){
                int nx = cur.x;
                int ny=cur.y+1;
                if(nx<0 || ny<0 || nx>=11 || ny>=11) continue;
                var sb = new StringBuilder();
                sb.append(cur.y);
                sb.append(cur.x);
                sb.append(ny);
                sb.append(nx);
                s.add(sb.toString());
                cur = new Pos(ny,nx);
            }
            else if(a=='L'){
                int nx = cur.x-1;
                int ny=cur.y;
                if(nx<0 || ny<0 || nx>=11 || ny>=11) continue;
                var sb = new StringBuilder();
                sb.append(ny);
                sb.append(nx);
                sb.append(cur.y);
                sb.append(cur.x);
                s.add(sb.toString());
                cur = new Pos(ny,nx);
            }
            else if(a=='R'){
                int nx = cur.x+1;
                int ny=cur.y;
                if(nx<0 || ny<0 || nx>=11 || ny>=11) continue;
                var sb = new StringBuilder();
                sb.append(cur.y);
                sb.append(cur.x);
                sb.append(ny);
                sb.append(nx);
                s.add(sb.toString());
                cur = new Pos(ny,nx);
            }

        }
        return s.size();
    }
}

 

* 개선

사실 정렬할필요없이 <from,to> <to,from>을 모두 넣고 size/2 하면 코드가 더 간단해진다.