알고리즘/백준

[백준/Swift/c++] 14891번: 톱니바퀴

녕이 2023. 3. 17. 22:34
728x90

 

https://www.acmicpc.net/problem/14891

 

14891번: 톱니바퀴

총 8개의 톱니를 가지고 있는 톱니바퀴 4개가 아래 그림과 같이 일렬로 놓여져 있다. 또, 톱니는 N극 또는 S극 중 하나를 나타내고 있다. 톱니바퀴에는 번호가 매겨져 있는데, 가장 왼쪽 톱니바퀴

www.acmicpc.net

 

톱니바퀴가 12시부터 시계방향으로 N/S 값이 배열로 입력된다.

우선 각 톱니바퀴의 방향을 dir 배열에 넣어주고, 톱니바퀴의 각 톱니의 값을 바꿔주는 방식으로 진행했다.

회전하는 톱니바퀴를 기준으로 left와 right 부분을 나눠서 진행했다. 각 방향으로 가던 중 돌아가지 않는다면( 같은 극의 경우 ) 끝낸다. 그 바퀴가 돌아가지 않으면 그 다음(혹은 이전)의 톱니바퀴도 돌아가지 않으므로

dir의 초기값은 0로 해줬다. 시계방향은 1, 반시계방향은 -1이므로 그 외는 돌아가지 않음을 나타낸다. 즉, 상태가 3개

이전에 c++로도 이 문제를 풀었었는데, 그때는 rotate 함수를 사용해서 톱니바퀴 배열을 회전시켰다.

 

Swift

import Foundation

//input
var wheel = [[Int]]()
for _ in 0..<4 { wheel.append(readLine()!.map{Int(String($0))!}) }
let k = Int(readLine()!)!

func rotationWheel(_ w: Int, _ d: Int) {
    var dir = [Int](repeating: 0, count: 4)
    dir[w] = d //톱니바퀴 w의 회전 방향
    var index = w
    
    //left
    while index > 0 && wheel[index][6] != wheel[index-1][2] {
        dir[index-1] = -dir[index] //반대 방향
        index -= 1
    }
    
    //right
    index = w
    while index < 3 && wheel[index][2] != wheel[index+1][6] {
        dir[index+1] = -dir[index]
        index += 1
    }
    
    for i in 0..<4 {
        if dir[i] == 1 { //시계방향
            let last = wheel[i][7]
            for j in (0...6).reversed() {
                wheel[i][j+1] = wheel[i][j]
            }
            wheel[i][0] = last
        }else if dir[i] == -1 {
            let first = wheel[i][0]
            for j in 1...7 {
                wheel[i][j-1] = wheel[i][j]
            }
            wheel[i][7] = first
        }
    }
}

func solution() {
    for _ in 0..<k {
        let input = readLine()!.split(separator: " ").map{ Int(String($0))! }
        let (w, d) = (input[0], input[1])
        rotationWheel(w-1, d)
    }
    var answer = 0
    for i in 0..<4 {
        answer += wheel[i][0] == 1 ? (1 << i) : 0
    }
    print(answer)
}
solution()

 

c++

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cmath>
using namespace std;

string wheel[4];
int k;

void RotationWheel(int w, int d){
    int dir[4] = {};
    dir[w] = d;
    int index = w;
    //left
    while(index > 0 && wheel[index][6] != wheel[index-1][2]) {
        dir[index-1] = -dir[index];
        index--;
    }
    //right
    index = w;
    while(index < 3 && wheel[index][2] != wheel[index+1][6]) {
        dir[index+1] = -dir[index];
        index++;
    }
    
    for(int i=0; i<4; i++){
        if(dir[i] == 1){ //시계방향
            rotate(wheel[i].begin(), wheel[i].begin()+7, wheel[i].end());
        }else if (dir[i] == -1){
            rotate(wheel[i].begin(), wheel[i].begin()+1, wheel[i].end());
        }
    }
}

int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    for(int i=0; i<4; i++) cin >> wheel[i];
    cin >> k;
    while(k--){
        int w, d;
        cin >> w >> d;
        RotationWheel(w-1, d);
    }
    int score = 0;
    for(int i=0; i<4; i++) if(wheel[i][0] == '1') score += (1 << i);
    cout << score << '\n';
    return 0;
}

 

 

728x90