[프로그래머스] 방문 길이 - (구현) c++

ha·2022년 1월 13일
0

프로그래머스

목록 보기
11/21

링크텍스트

좌표 방문 여부 x -> 간선 방문 여부

//fromX, fromY, toX, toY
int board[11][11][11][11];

using namespace std;
int visited[12][12];
int solution(string dirs) {
    int answer = 0;
    int nx=5;
    int ny=5;
    
    for(auto c : dirs){
        
        if(c=='U'){
            nx=nx-1;
            if(nx<0) {
                nx+=1;
                continue;
            }
        }
        else if(c=='L'){
            ny=ny-1;
            if(ny<0) {
                ny+=1;
                continue;
            }
        }
        else if(c=='D'){
            nx=nx+1;
            if(nx>10) 
            {
                nx-=1;
                continue;
            }
        }
        else{
            ny=ny+1;
            if(ny>10) 
            {
                ny-=1;
                continue;
            }
        }
        
       if(!visited[nx][ny]){
            visited[nx][ny]=1;
            answer++;
        } 
    }
    return answer;
}

0개의 댓글