본문 바로가기

Java/Java 알고리즘 LeetCode

[LeetCode- Part. 2] 1. 원 안에서 로봇 # (+ toCharArray)

반응형

 

https://leetcode.com/problems/robot-bounded-in-circle/

 

Robot Bounded In Circle - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

 

로봇이 원을 그리는지 확인

로봇은 처음(0, 0) 북쪽을 향한다.
로봇은 세 가지 명령 수행 가능

"G": 1 단위 직진
"L":
왼쪽으로 90도 회전
"R":
오른쪽으로 90도 회전

로봇은 주어짂 것을 순서대로 수행하면서 무한반복

 로봇이 평면에 원을 그리는 경우 true를 리턴

(1) 직진을 하지 않거나

(2) 북쪽을 가리키는 경우 

-> 원을 그리지 않는다.

 

즉, 좌표 이동과 방향 이동 두가지로 분류

1.G일 경우 -> 좌표 이동

2. Left일 경우 -> N, E, S, W 순서

3.Right일 경우 -> N,W, S, E 순서

 

원을 만드는 2가지 경우 

1. 좌표 이동으로 원점으로 갈 경우

2. 방향 이동으로 북쪽이 아닐 경우

 

class Solution {
    public boolean isRobotBounded(String instructions) {
        //시작은 북쪽
        
        //L일 경우
        //0 북쪽 (0,1)
        //1 왼쪽 (-1,0)
        //2 오른쪽 (1,0)
        //3 아래쪽 (0,-1)
        int[] robot={0,0};
        int[][] left={{0,1}, {-1,0},{1,0},{0,-1}};
        //R일 경우
        //0 북쪽 (0,1)
        //1 오른쪽 (1,0)
        //2 아래쪽 (0,-1)
        //3 왼쪽 (-1,0)
        int[][] right={{0,1},{1,0},{0,-1},{-1,0}};
        char dir='N';
        int x=0;
        int y=0;
        
        if(instructions==null || instructions.length()==0) return false;
        
        for(char c:instructions.toCharArray()){
            if(c=='G'){
                if(dir=='N') y+=1;
                else if(dir=='W') x-=1;
                else if(dir=='S') y-=1;
                else if(dir=='E') x+=1;
                System.out.println(x+" "+y);
            }
            else if(c=='L'){
                if(dir=='N') dir='W';
                else if(dir=='W') dir='S';
                else if(dir=='S') dir='E';
                else if(dir=='E')dir='N';
            }
            else if(c=='R'){
                if(dir=='N') dir='E';
                else if(dir=='E') dir='S';
                else if(dir=='S') dir='W';
                else if(dir=='W') dir='N';
            }
        }
        
        if(x==0 && y==0) return true;
        System.out.println("결과"+dir);
        //G 같은 방향 1칸 이동, L 반시계방향, R 시계방향
        if(dir=='N') return false;
        return true;
    }
}

 

예외처리 필수

if(instructions==null || instructions.length()==0) return false;

 

원을 도는 경우

1. 명령을 모두 이동했을 때 북쪽을 가리키지 않을 때

2. 명령을 모두 이동했을 때 원점으로 왔을 경우

 

원을 돌지 않는 경우

1. 이동할 명령이 존재하지 않을 경우

2. 마지막 방향이 북을 가리킬 경우

3. 명령이 끝났을 때 원점으로 오지 않는 경우

 

class Solution {
    public boolean isRobotBounded(String instructions) {
        //명령이 끝났을 때 북쪽을 향하지 않으면 모두 참
        char d='N';
        int[] s={0,0};
        //북, 서, 남, 동
        int[][] dirs={{0,1},{-1,0},{0,-1},{1,0}};
        for(char c:instructions.toCharArray()){
            if(c=='G'){
                if(d=='N'){
                    s[0]+=dirs[0][0];
                    s[1]+=dirs[0][1];
                    //System.out.println(s[0]+" "+s[1]);
                }else if(d=='W'){
                    s[0]+=dirs[1][0];
                    s[1]+=dirs[1][1];
                    //System.out.println(s[0]+" "+s[1]);
                }else if(d=='S'){
                    s[0]+=dirs[2][0];
                    s[1]+=dirs[2][1];
                    //System.out.println(s[0]+" "+s[1]);
                }else if(d=='E'){
                    s[0]+=dirs[3][0];
                    s[1]+=dirs[3][1];                                 
                    //System.out.println(s[0]+" "+s[1]);
                }
            }
            else if(c=='L'){
                if(d=='N'){
                    d='W';                
                }else if(d=='W'){
                    d='S';
                }else if(d=='S'){
                    d='E';
                }else if(d=='E'){
                    d='N';
                }
            }
            else if(c=='R'){
                if(d=='N'){
                    d='E';
                }else if(d=='E'){
                    d='S';
                }else if(d=='S'){
                    d='W';
                }else if(d=='W'){
                    d='N';
                }
            }
        }
        if(s[0]==0 && s[1]==0) return true;
        
        return d=='N'?false:true;
    }
}

 

-> 좌표 변수를 int[]에서 x,y로 변경

 

class Solution {
    public boolean isRobotBounded(String instructions) {
        //명령이 끝났을 때 북쪽을 향하지 않으면 모두 참
        char d='N';
        
        //int[] 대신 x,y로 변경
        int x=0;
        int y=0;
        //int[] s={0,0};
        //북, 서, 남, 동
        int[][] dirs={{0,1},{-1,0},{0,-1},{1,0}};
        for(char c:instructions.toCharArray()){
            if(c=='G'){
                if(d=='N'){
                    
                    x+=dirs[0][0];
                    y+=dirs[0][1];
                    //System.out.println(s[0]+" "+s[1]);
                }else if(d=='W'){
                    x+=dirs[1][0];
                    y+=dirs[1][1];
                    //System.out.println(s[0]+" "+s[1]);
                }else if(d=='S'){
                    x+=dirs[2][0];
                    y+=dirs[2][1];
                    //System.out.println(s[0]+" "+s[1]);
                }else if(d=='E'){
                    x+=dirs[3][0];
                    y+=dirs[3][1];                                 
                    //System.out.println(s[0]+" "+s[1]);
                }
            }
            else if(c=='L'){
                if(d=='N'){
                    d='W';                
                }else if(d=='W'){
                    d='S';
                }else if(d=='S'){
                    d='E';
                }else if(d=='E'){
                    d='N';
                }
            }
            else if(c=='R'){
                if(d=='N'){
                    d='E';
                }else if(d=='E'){
                    d='S';
                }else if(d=='S'){
                    d='W';
                }else if(d=='W'){
                    d='N';
                }
            }
        }
        if(x==0 && y==0) return true;
        
        return d=='N'?false:true;
    }
}
반응형