개발자 김수진

14891(톱니바퀴) 본문

알고리즘/백준

14891(톱니바퀴)

김수진장 2020. 4. 30. 11:16
#include <iostream>

using namespace std;

//1인 경우는 시계 방향이고, -1인 경우는 반시계 방향
// N극은 0, S극은 1로 나타나있다.

int K; //회전 횟수
int wheel[4][8];
int tmp[4][8];


void rotate(int idx, int dir)
{
    if(dir ==1 ){
        for(int i=0;i<7;i++)
            wheel[idx][i+1] = tmp[idx][i];
        wheel[idx][0] = tmp[idx][7];
    }
    
    else{
        for(int i=1;i<8;i++)
            wheel[idx][i-1] = tmp[idx][i];
        wheel[idx][7] = tmp[idx][0];
    }
}


int check(int idx)
{
    if(idx == 1){
        if(tmp[0][2] == tmp [1][6]) return 0;
        else return 1;
    }
    else if(idx == 2){
        if(tmp[1][2] == tmp[2][6])  return 0;
        else return 1;
    }
    else if (idx ==3){
        if(tmp[2][2] == tmp[3][6])  return 0;
        else return 1;
    }
    
    return 0;
    
}

int main(void)
{
    
    for(int i=0;i<4;i++){
        for(int j=0;j<8;j++)
            scanf("%1d",&wheel[i][j]);
    }
    
    cin >> K;
    
    for(int i=0;i<K;i++){
        
        for(int i=0;i<4;i++){
            for(int j=0;j<8;j++)
                tmp[i][j]=wheel[i][j];
        }
        int idx,dir;
        cin >> idx >> dir;
        idx--;
        
        rotate(idx,dir);
        
        switch(idx)
        {
            case 0:
                while(1){
                    idx++;
                    if(idx == 4)    break;
                    
                    if(dir == 1)    dir =0;
                    else    dir =1;
                    
                    if(check(idx))  rotate(idx,dir);
                    else    break;
                }
                break;
                
            case 1:
                if(check(1)){
                    if(dir == 1)    rotate(0,0);
                    else    rotate(0,1);
                }
                
                while(1){
                    idx++;
                    if(idx == 4)    break;
                    
                    if(dir == 1)    dir =0;
                    else    dir =1;
                    
                    if(check(idx))  rotate(idx,dir);
                    else    break;
                }
                break;
                
            case 2:
                if(check(3)){
                    if(dir == 1)    rotate(3,0);
                    else    rotate(3,1);
                }
                
                while(1){
                    idx--;
                    if(idx == -1)    break;
                    
                    if(dir == 1)    dir =0;
                    else    dir =1;
                    
                    if(check(idx+1))  rotate(idx,dir);
                    else    break;
                }
                break;
                
            case 3:
                while(1){
                    idx--;
                    if(idx == -1)    break;
                    
                    if(dir == 1)    dir =0;
                    else    dir =1;
                    
                    if(check(idx+1))  rotate(idx,dir);
                    else    break;
                }
                break;
        }
    }
    //1번 톱니바퀴의 12시방향이 N극이면 0점, S극이면 1점

    int total=0;
    
    if(wheel[0][0]) total+=1;
    if(wheel[1][0]) total+=2;
    if(wheel[2][0]) total+=4;
    if(wheel[3][0]) total+=8;

    cout << total << "\n";
    
    return 0;
}

 

한번 풀었던 문제인데 예전에는 '틀렸습니다!'가 나왔지만 이번에는 맞았다.

이렇게 조건 하나씩 따져가면서 직접 다 구현하는 문제를 시뮬레이션이라고 하는 것 같다.

아직 시뮬레이션이 제일 막막하다.

다른 알고리즘들은 방법에 맞게 풀면 될텐데 이거는 하나씩 다 따지는 방법밖에 없으니 조건 잘 따져가면서 풀어야 할듯!

 

그리고 처음에 input 값 제대로 안따져줘서 틀렸다. 

 

'알고리즘 > 백준' 카테고리의 다른 글

15652-N과M(4)  (0) 2020.05.11
15651 - N과M(3)  (0) 2020.05.11
15650 - N과M(2)  (0) 2020.05.11
15649 - N과 M(1)  (0) 2020.05.11
백준 - 2667(단지번호붙이기)  (0) 2020.04.26