입력 : 톱니바퀴 모양, 회전 횟수와 회전할 바퀴, 방향
출력 : 회전 후 톱니의 모양
풀이
1. 톱니모양을 나타내는 값 사이에 공백이 없어 int로 받을 시 0과 1을 8개받는게 아닌 8자리 수로 입력됨
-> 문자열로 입력받아 문자열로 모두 처리
-> 8자리 수로 받은 뒤 각 자리 수를 저장(채택)
2. 톱니바퀴를 돌리기 전, 좌우 톱니바퀴를 돌려야 하는지 확인한 후 돌려야하면 재귀실행
-> 무한반복할 수 있으니 한번 돌린 바퀴는 전역변수로 표시해주기
-> 맨 왼쪽이나 맨 오른쪽 범위 계산
3. 좌우 톱니바퀴는 돌리기 전 상태에서 확인해야 하기 때문에 재귀실행 후에 바퀴를 돌려야 한다.
소스코드
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 |
#include <iostream>
using namespace std;
int wheel[4][8];
bool wheelTurned[4];
void turnWheel(int wheelNum, int direction){
int temp[8];
for(int i=0; i<8; i++)
temp[i] = wheel[wheelNum][(8 + direction + i) % 8];
for(int i=0; i<8; i++)
wheel[wheelNum][i] = temp[i];
}
void turnCheck(int wheelNum, int direction){
if(wheelTurned[wheelNum])
return;
wheelTurned[wheelNum] = true;
if(wheelNum - 1 >= 0)
if(wheel[wheelNum-1][2] != wheel[wheelNum][6])
turnCheck(wheelNum-1, direction*(-1));
if(wheelNum + 1 <= 3)
if(wheel[wheelNum+1][6] != wheel[wheelNum][2])
turnCheck(wheelNum+1, direction*(-1));
turnWheel(wheelNum, direction);
wheelTurned[wheelNum] = false;
}
int main() {
int roundNum;
int result = 0;
for(int i=0; i<4; i++){
int temp;
cin>>temp;
for(int j=7; j>=0; j--){
wheel[i][j] = temp % 10;
temp /= 10;
}
}
cin>>roundNum;
for(int i=0; i<roundNum; i++){
int wheelNum, direction;
cin>>wheelNum>>direction;
turnCheck(wheelNum-1, (-1)*direction);
}
result = wheel[0][0] + wheel[1][0]*2 + wheel[2][0]*4 + wheel[3][0]*8;
cout<<result<<endl;
return 0;
} |
cs |
'개발 > 알고리즘' 카테고리의 다른 글
백준 알고리즘 14888 (0) | 2019.02.28 |
---|---|
백준 알고리즘 14503 (0) | 2019.02.21 |
백준 알고리즘 1673 (0) | 2019.02.20 |
백준 알고리즘 14889 (0) | 2019.02.19 |
백준 알고리즘 14500 (0) | 2019.02.19 |