Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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
Archives
Today
Total
관리 메뉴

최's 먹공로그

SEA4013_[모의 SW 역량테스트]특이한 자석 본문

APS

SEA4013_[모의 SW 역량테스트]특이한 자석

ChoiSH313 2019. 8. 17. 18:56

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWIeV9sKkcoDFAVH


문제정리

1. 4개의 자석 , 각 자석은 8개의 튀어나온 곳을 가지고 있다.

2. 자석은 각 날 마다 N극 또는 S 극의 자성을 가지고 있다.

3. 자석의 신기한 규칙

(1) 임의의 자석을 1칸씩 K번 회전 시킬 때

(2) 하나의 자석이 1 칸 회전될 때, 붙어 있는 자석은 서로 붙어 있는 날의

자성과 다를 경우에만(돌리기 전) 인력에 의해 반대 방향으로 1 칸 회전된다.

4. 점수 계산

(1) 1번 자석에서 빨간색 화살표 위치에 있는 날의 자성이 N 0 , S 1

(2) 2번 자석에서 빨간색 화살표 위치에 있는 날의 자성이 N 0 , S 2

(3) 3번 자석에서 빨간색 화살표 위치에 있는 날의 자성이 N 0 , S 4

(4) 4번 자석에서 빨간색 화살표 위치에 있는 날의 자성이 N 0 , S 8

5. 4개 자석의 자성 정보와 자석을 1 칸씩 K 번 회전시키려고 할 때,

K번 자석을 회전시킨 후 획득하는 점수의 총 합을 출력하는 프로그램을 작성하라.

6. 자석을 회전시키는 방향은 시계방향이 1 , 반시계 방향이 -1로 주어진다.

7. 날의 자성은 N극이 0 , S극이 1

8. 각 자석의 날 자성정보는 빨간색 화살표 위치의 날부터 시계방향 순서대로 주어진다.


문제issue

1. 자석 rotation

2. 인접한 날의 인덱스


해결흐름

1. 각 자석의 일차원 배열을 만든다. 0번째가 빨간색 화살표 위치이다.

2. 돌리기 전에 인접하는 자석과의 극 정보를 확인한다.

앞의 자석 index 2와 뒤의 자석 index 6이 인접한 날이다.

3. 돌리기

(1) 마지막 인덱스의 값을 temp에 빼고 뒤로 한칸씩 밀고

(2) temp를 첫번째 인덱스에 넣는다.

4. K번의 회전이 끝난뒤 점수를 합한다.


소스코드

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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
 
public class Solution {
 
    public static void main(String[] args) throws Exception {
        // input
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int test_case = Integer.parseInt(br.readLine().trim());
        for (int tc = 1; tc <= test_case; tc++) {
            int K = Integer.parseInt(br.readLine().trim());
            int[][] magnet = new int[5][8]; // 1~4번 자석 , 0~7날
            for (int i = 1; i < 5; i++) {
                StringTokenizer st = new StringTokenizer(br.readLine().trim());
                for (int j = 0; j < 8; j++) {
                    magnet[i][j] = Integer.parseInt(st.nextToken());
                }
            }
            
            for (int i = 0; i < K; i++) {
                StringTokenizer st = new StringTokenizer(br.readLine().trim());
                int m_number = Integer.parseInt(st.nextToken());
                int m_dir = Integer.parseInt(st.nextToken());
                if(m_number == 1) {
                    int[] m_rotation = new int[5]; // 1,2,3,4 사용
                    m_rotation[1= m_dir;
                    if(magnet[1][2!= magnet[2][6]) {
                        if(m_dir == 1)
                            m_rotation[2= -1;
                        else if(m_dir == -1)
                            m_rotation[2= 1;
                        if(magnet[2][2!= magnet[3][6]) {
                            if(m_rotation[2== 1)
                                m_rotation[3= -1;
                            else if(m_rotation[2== -1)
                                m_rotation[3= 1;
                            if(magnet[3][2!= magnet[4][6]) {
                                if(m_rotation[3== 1)
                                    m_rotation[4= -1;
                                else if(m_rotation[3== -1)
                                    m_rotation[4= 1;
                            }
                        }
                    }
                    // 한번에 돌림
                    for (int j = 1; j < 5; j++) {
                        if(m_rotation[j] == 1) {
                            int temp = magnet[j][7];
                            for (int l = 6; l >= 0; l--) {
                                magnet[j][l+1= magnet[j][l];
                            }
                            magnet[j][0= temp;
                        } else if(m_rotation[j] == -1) {
                            int temp = magnet[j][0];
                            for (int l = 1; l < 8; l++) {
                                magnet[j][l-1= magnet[j][l];
                            }
                            magnet[j][7= temp;
                        }
                    }
                } else if(m_number == 2) {
                    int[] m_rotation = new int[5];
                    m_rotation[2= m_dir;
                    if(magnet[2][6!= magnet[1][2]) {
                        if(m_rotation[2== 1)
                            m_rotation[1= -1;
                        else if(m_rotation[2== -1)
                            m_rotation[1= 1;
                    }
                    if(magnet[2][2!= magnet[3][6]) {
                        if(m_rotation[2== 1)
                            m_rotation[3= -1;
                        else if(m_rotation[2== -1)
                            m_rotation[3= 1;
                        if(magnet[3][2!= magnet[4][6]) {
                            if(m_rotation[3== 1)
                                m_rotation[4= -1;
                            else if(m_rotation[3== -1)
                                m_rotation[4= 1;
                        }
                    }
                    // 돌리기
                    for (int j = 1; j < 5; j++) {
                        if(m_rotation[j] == 1) {
                            int temp = magnet[j][7];
                            for (int l = 6; l >= 0; l--) {
                                magnet[j][l+1= magnet[j][l];
                            }
                            magnet[j][0= temp;
                        } else if(m_rotation[j] == -1) {
                            int temp = magnet[j][0];
                            for (int l = 1; l < 8; l++) {
                                magnet[j][l-1= magnet[j][l];
                            }
                            magnet[j][7= temp;
                        }
                    }
                } else if(m_number == 3) {
                    int[] m_rotation = new int[5];
                    m_rotation[3= m_dir;
                    if(magnet[3][2!= magnet[4][6]) {
                        if(m_rotation[3== 1)
                            m_rotation[4= -1;
                        else if(m_rotation[3== -1)
                            m_rotation[4= 1;
                    }
                    if(magnet[3][6!= magnet[2][2]) {
                        if(m_rotation[3== 1)
                            m_rotation[2= -1;
                        else if(m_rotation[3== -1)
                            m_rotation[2= 1;
                        if(magnet[2][6!= magnet[1][2]) {
                            if(m_rotation[2== 1)
                                m_rotation[1= -1;
                            else if(m_rotation[2== -1)
                                m_rotation[1= 1;
                        }
                    }
                    // 돌리기
                    for (int j = 1; j < 5; j++) {
                        if(m_rotation[j] == 1) {
                            int temp = magnet[j][7];
                            for (int l = 6; l >= 0; l--) {
                                magnet[j][l+1= magnet[j][l];
                            }
                            magnet[j][0= temp;
                        } else if(m_rotation[j] == -1) {
                            int temp = magnet[j][0];
                            for (int l = 1; l < 8; l++) {
                                magnet[j][l-1= magnet[j][l];
                            }
                            magnet[j][7= temp;
                        }
                    }
                } else if(m_number == 4) {
                    int[] m_rotation = new int[5];
                    m_rotation[4= m_dir;
                    if(magnet[4][6!= magnet[3][2]) {
                        if(m_rotation[4== 1)
                            m_rotation[3= -1;
                        else if(m_rotation[4== -1)
                            m_rotation[3= 1;
                        if(magnet[3][6!= magnet[2][2]) {
                            if(m_rotation[3== 1)
                                m_rotation[2= -1;
                            else if(m_rotation[3== -1)
                                m_rotation[2= 1;
                            if(magnet[2][6!= magnet[1][2]) {
                                if(m_rotation[2== 1)
                                    m_rotation[1= -1;
                                else if(m_rotation[2== -1)
                                    m_rotation[1= 1;
                            }
                        }
                    }
                    // 돌리기
                    for (int j = 1; j < 5; j++) {
                        if(m_rotation[j] == 1) {
                            int temp = magnet[j][7];
                            for (int l = 6; l >= 0; l--) {
                                magnet[j][l+1= magnet[j][l];
                            }
                            magnet[j][0= temp;
                        } else if(m_rotation[j] == -1) {
                            int temp = magnet[j][0];
                            for (int l = 1; l < 8; l++) {
                                magnet[j][l-1= magnet[j][l];
                            }
                            magnet[j][7= temp;
                        }
                    }
                }
            }
            // 점수 측정
            int sum = 0;
            for (int i = 1; i < 5; i++) {
                if(i == 1 && magnet[i][0== 1)
                    sum += 1;
                else if(i == 2 && magnet[i][0== 1)
                    sum += 2;
                else if(i == 3 && magnet[i][0== 1)
                    sum += 4;
                else if(i == 4 && magnet[i][0== 1)
                    sum += 8;
            }
            System.out.println("#" + tc + " " + sum);
        } // end of tc
    } // end of main
// end of class
cs