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

최고를 향해 최선을 다하자

[SWEA] 4013. 특이한 자석 본문

SWEA

[SWEA] 4013. 특이한 자석

응애개발자 2020. 6. 5. 22:41

안녕? 나는 응애개발자

 

 

문제를 보고 '아 뭐야? 어렵지 않아서 대-충 풀면 알아서 되겠네?' 싶어

문제도 대-충읽고 대-충 로직짜서 코딩해서 돌렸는데

틀린것이다;;

 

알고보니 문제를 대충읽어서 로직을 완전 반대로 짜버렸다ㅋㅋ (이걸로 한시간 삽질함)

문제를 완벽하게 이해하는건.. 중요하다는 것을 다시한번 느꼈다...큐ㅠㅠ

 

각설, 코드 리뷰 시작!

오늘의 코드는 더럽다. 어쩔수 없다 난 응애니까!

#include <iostream>
#include <vector>
#include <deque>
#include <queue>
using namespace std;

int K;
int magnet[4][8];
queue<pair<int, int> > rotation;
int mag_match[4][2] = {{-1,1},{0,2},{1,3},{2,-1}};


//2^n을 구하는 함수
int multi(int n){
    int ans = 1;
    for(int i=1; i<=n; i++){
        ans *= 2;
    }
    return ans;
}

//판 돌리는 함수
void rotate_magnet(int mag, int dir){
    if(dir == 1){
        int temp7 = magnet[mag][7];

        int temp = magnet[mag][0];
        int next;
        for(int i=1; i<8; i++){
            next = magnet[mag][i];
            magnet[mag][i] = temp;
            temp = next;

        }
        magnet[mag][0] = temp7;
    }
    
    else if(dir == -1){
        int temp0 = magnet[mag][0];

        int temp = magnet[mag][7];
        int next;
        for(int i=6; i>=0; i--){
            next = magnet[mag][i];
            magnet[mag][i] = temp;
            temp = next;
        }
        magnet[mag][7] = temp0;
    }
    return;
}

//현재방향의 반대방향을 리턴하는 함수
int conv_dir(int dir){
    if(dir == 1)
        return -1;
    return 1;
}

//현재 판과 비교할 판을 받아서 결과를 리턴해주는 함수
bool mag_check(int cur_mag, int mag){
    int result = false;
    if((cur_mag==0) && (mag==1)){
        if(magnet[cur_mag][2] != magnet[mag][6])
            result = true;
    }
    else if((cur_mag==1) && (mag==0)){
        if(magnet[cur_mag][6] != magnet[mag][2])
            result = true;
    }
    else if((cur_mag==1) && (mag==2)){
        if(magnet[cur_mag][2] != magnet[mag][6])
            result = true;
    }
    else if((cur_mag==2) && (mag==1)){
        if(magnet[cur_mag][6] != magnet[mag][2])
            result = true;
    }
    else if((cur_mag==2) && (mag==3)){
        if(magnet[cur_mag][2] != magnet[mag][6])
            result = true;
    }
    else if((cur_mag==3) && (mag==2)){
        if(magnet[cur_mag][6] != magnet[mag][2])
            result = true;
    }
    return result;
}

void sol(){
    int size = rotation.size();

    for(int i=0; i<size; i++){
        int cur_magnet = rotation.front().first;
        int cur_dir = rotation.front().second;
        rotation.pop();

        queue<pair<int, int> > q;	//돌려야하는 판을 탐색하기위한 queue
        queue<pair<int, int> > qq;	//돌려야하는 판의 정보를 넣기위한 queue
        int check[4] = {0,};		//한번 탐색한 판을 안탐색하려고 만든 check함수
        
        check[cur_magnet] = 1;
        q.push(make_pair(cur_magnet, cur_dir));
        qq.push(make_pair(cur_magnet, cur_dir));

        while(!q.empty()){
            cur_magnet = q.front().first;
            cur_dir = q.front().second;
            for(int i=0; i<2; i++){
                int next = mag_match[q.front().first][i];
                    
                if(mag_match[q.front().first][i] == -1)
                    continue;
                
                if(mag_check(cur_magnet,next) && check[next]!=1){
                    check[cur_magnet] = 1;
                    q.push(make_pair(next,conv_dir(cur_dir)));
                    qq.push(make_pair(next,conv_dir(cur_dir)));
                }
            }
            q.pop();      
        }
        
        while(!qq.empty()){
            rotate_magnet(qq.front().first, qq.front().second);
            qq.pop();
        }
    }
}


int main(int argc, char** argv){
    int test_case;
    cin>>test_case;

    for(int T=1; T<=test_case; T++){
        cin>>K;
        for(int i=0; i<4; i++){
            for(int j=0; j<8; j++){
                cin>>magnet[i][j];
            }
        }
        for(int i=0; i<K; i++){
            int m,dir;
            cin>>m>>dir;
            rotation.push(make_pair(m-1,dir));
        }
        
        sol();
        
        int ans = 0;
        for(int i=0; i<4; i++){
            if(magnet[i][0] == 1){
                ans += multi(i);
            }
        }
        cout<<"#"<<T<<" "<<ans<<endl;
    }

    return 0;
}

 

'SWEA' 카테고리의 다른 글

[SWEA] 2382. 미생물 격리  (0) 2020.06.06
[SWEA] 2105. 디저트 카페  (0) 2020.06.06
[SWEA] 2117. 홈 방범 서비스  (0) 2020.06.05
Comments