콘솔창 & 윈도우창/코딩 테스트

프로그래머스 LV.2 방문 길이

뽀또치즈맛 2025. 4. 17. 22:19

 

 

GitHub : 

https://github.com/kwon1232/CodingTest/tree/main/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/2/49994.%E2%80%85%EB%B0%A9%EB%AC%B8%E2%80%85%EA%B8%B8%EC%9D%B4

#include <string>
#include <cstring>

using namespace std;

int solution(string dirs) {
    int answer = 0;
    
    int dx[4] = {0, -1, 0, 1};
    int dy[4] = {-1, 0, 1, 0};
    
    bool visited[11][11][4];
    memset(visited, false, sizeof(visited));
    
    int x = 5, y = 5;
    
    for (int i = 0; i < dirs.size(); i++)
    {
        int dir = 0;
        if(dirs[i] == 'L')
        {
            dir = 1;
        }
        else if(dirs[i] == 'U')
        {
            dir = 2;
        }
        else if(dirs[i] == 'R')
        {
            dir = 3;
        }
        else if (dirs[i] == 'D')
        {
            dir = 0;
        }
        
        int nx = x + dx[dir];
        int ny = y + dy[dir];
        
        if (nx < 0 || ny < 0 || nx >= 11 || ny >= 11) continue;
        int countdir = (dir + 2) % 4;
        if(!visited[x][y][dir] && !visited[nx][ny][countdir])
        {
            visited[x][y][dir] = true;
            visited[nx][ny][countdir] = true;
            answer++;
        }
        x = nx;
        y = ny;
    }
    
    
    return answer;
}

 

 

문제 출저 : https://school.programmers.co.kr/learn/courses/30/lessons/49994

 

프로그래머스

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

programmers.co.kr