티스토리 뷰

반응형


#. Problem

* The copyright in this matter is in BOJ


#. Resolution Process

  1. Read and understand problem

  2. Redefine the problem + abstract

  3. Create solution plan (select Algorithm, Data structure)

  4. Prove the plan (check performance time and usage memory)

  5. Carry out the plan

  6. Look back on the plan and find a way to improve it


#. Solve

 

이 문제는 좌표가 우리가 흔히 사용하는 좌표와 반대여서 헷갈리다ㅠ

사실 좌표때문에 헷갈려서 살짝 틀렸다...ㅋ..ㅋ


우선 체크해야할 조건들은 아래와 같다.

각각의 명령은 순차적으로 실행

L: 로봇이 향하고 있는 방향을 기준으로 왼쪽으로 90도 회전

  R: 로봇이 향하고 있는 방향을 기준으로 오른쪽으로 90도 회전

  F: 로봇이 향하고 있는 방향을 기준으로 앞으로 한 칸 움직


나는 흔히 사용하는 좌상단 (0,0)을 기준으로 하였다.

그래서 반대로 생각해야하는 부분이 살짝 많긴(?)하다.

북 -> 남

남 -> 북

오른쪽으로 90도 -> 왼쪽으로 90도

왼쪽으로 90도 -> 오른쪽으로 90도


이렇게 바꿔서 생각해주어야 한다.


#. Code


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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class BOJ2174 {
 
    static int A, B, N, M;
    static int map[][];
    static int[] dir = { 0123 }; // 북, 동, 남, 서
    static int[] dx = {-1010}, dy = {010-1};
    static Robot[] robots;
    
    static class Robot {
        int x, y, dir;
 
        public Robot(int x, int y, int dir) {
            super();
            this.x = x;
            this.y = y;
            this.dir = dir;
        }
        
    }
    
    public static void main(String[] args) throws IOException {
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
 
        A = Integer.parseInt(st.nextToken()); // 가로
        B = Integer.parseInt(st.nextToken()); // 세로
        map = new int[B][A];
 
        st = new StringTokenizer(br.readLine());
        N = Integer.parseInt(st.nextToken()); // 로봇 개수
        M = Integer.parseInt(st.nextToken()); // 명령 개수
        robots = new Robot[N + 1];
 
        // 로봇 세팅
        for (int i = 1; i <= N; i++) {
            st = new StringTokenizer(br.readLine());
            int b = Integer.parseInt(st.nextToken()) - 1// X
            int a = Integer.parseInt(st.nextToken()) - 1// Y
            char tmp = st.nextToken().charAt(0); // 방향
            // 방향 설정 북->남, 남->북
            int d = 0;
            if (tmp == 'N') d = 2;
            else if (tmp == 'E') d = 1;
            else if (tmp == 'S') d = 0;
            else d = 3;
 
            map[a][b] = i;
            robots[i] = new Robot(a, b, d);
        }
 
        // 명령
        String res = "";
        for (int i = 0; i < M; i++) {
            st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken());
            char m = st.nextToken().charAt(0); // 동작
            int g = Integer.parseInt(st.nextToken()); // 반복
            
            res = go(a, m, g);
            // 잘못된 명령일 경우
            if(!res.equals("OK")) {
                System.out.println(res);
                break;
            }
        }
        
        if(res.equals("OK")) System.out.println(res);
    }
 
    private static String go(int n, char motion, int move) {
 
        Robot now = robots[n];
        
        // L : 오른쪽으로 90도 회전
        if (motion == 'L'
            robots[n].dir = (now.dir + move) % 4;
        // R : 왼쪽으로 90도 회전
        else if (motion == 'R'
            robots[n].dir = (now.dir + move * 3) % 4;
        // F : 전진
        else {
            int xx = now.x, yy = now.y;
            for (int i = 0; i < move; i++) {
                xx += dx[now.dir];
                yy += dy[now.dir];
                // X번 로봇이 벽에 충돌
                if(xx < 0 || xx >= B || yy < 0 || yy >= A)
                    return "Robot " + n + " crashes into the wall";
                // X번 로봇이 Y번 로봇에 충돌
                if(map[xx][yy] != 0
                    return "Robot "+ n + " crashes into robot " + map[xx][yy];
            }
            
            // 로봇 정보 갱신
            robots[n] = new Robot(xx, yy, now.dir);
            // 로봇 이동
            map[now.x][now.y] = 0;
            map[xx][yy] = n;
        }
        
        return "OK";
    }
 
}
cs


line 14~24) 로봇 정보를 저장하는 class

line 47~51) 맵을 뒤집어서 생각해야하므로 방향도 뒤집어주어야 한다.

line 53) 맵에 로봇을 위치

line 54) 로봇 정보 저장


line 82) 방향을 뒤집었으니까 L 명령을 받았을 경우 오른쪽으로 90도 회전

line 85) 방향을 뒤집었으니까 R 명령을 받았을 경우 왼쪽으로 90도 회전

         현재 위치에서 + 3 만큼 돌려주면 왼쪽으로 1번 돌린 것과 같은 효과

line 89~98) 전진하면서 벽이나 로봇에 충돌하는지 확인

line 101~104) 충돌하지 않았다면 로봇 정보를 갱신하고 이동

반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday