슈콩

[프로그래머스] Lv.2 방문 길이 본문

Algorithms/Programmers

[프로그래머스] Lv.2 방문 길이

shukong 2025. 10. 16. 01:23

 

 

[문제]

https://school.programmers.co.kr/learn/courses/30/lessons/49994

 

프로그래머스

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

programmers.co.kr

 

 

[소스 코드]

class Solution {
    int[] dy = {1,0,-1,0};
    int[] dx = {0,1,0,-1};
    public int solution(String dirs) {
        int answer = 0;
        boolean[][][] visit = new boolean[11][11][4];
        int y = 5; int x = 5;
        int d = 0;
        for(char c : dirs.toCharArray()){
            if(c=='U') d = 0;
            if(c=='R') d = 1;
            if(c=='D') d = 2;
            if(c=='L') d = 3;
            int ny = y + dy[d];
            int nx = x + dx[d];
            if(ny<0 || ny>=11 || nx<0 || nx>=11) continue;
            if(!visit[ny][nx][d]){
                visit[ny][nx][d] = true;
                answer++;
            }
            visit[y][x][(d+2) % 4] = true;
            y = ny; x = nx;
        }
        return answer;
    }
}