Gidhub BE Developer

[SW Expert Academy] - 특이한 자석

2018-05-10
goodGid

Problem

Problem URL : 특이한 자석


[1] Answer Code (18. 08. 24)


#include<iostream>
#include<cstring>
using namespace std;

int m[4][8];
int chk[4];
void tob_rotate(int idx, int dir){
    int tmp;
    if(dir == 1){
        tmp = m[idx][7];
        for(int i=7; i>=0; i--){
            m[idx][i] = m[idx][i-1];
        }
        m[idx][0] = tmp;
    }
    else {
        tmp = m[idx][0];
        for(int i=0; i<7; i++){
            m[idx][i] = m[idx][i+1];
        }
        m[idx][7] = tmp;
    }
}

void dfs(int idx, int dir){
    if( chk[idx] ) {
        return ;
    }
    chk[idx] = 1;
    if( idx == 0 ){
        if( m[idx][2] != m[idx+1][6]){
            dfs(idx+1, -1 * dir);
        }
    }
    else if( idx == 1 || idx == 2) {
        if( m[idx][2] != m[idx+1][6]){
            dfs(idx+1, -1 * dir);
        }
        
        if( m[idx][6] != m[idx-1][2]){
            dfs(idx-1, -1 * dir);
        }
    }
    else if( idx == 3){
        if( m[idx][6] != m[idx-1][2]){
            dfs(idx-1, -1 * dir);
        }
    }
    tob_rotate(idx, dir);
}

int main(){
    int TC;
    cin >> TC;
    for (int tc=1; tc<=TC; tc++) {
        int k;
        cin >> k;
        memset(m,0,sizeof(m));
        for(int i=0; i<4; i++) for(int j=0; j<8; j++) scanf("%d",&m[i][j]);
        
        for(int i=0; i<k; i++){
            int idx,dir;
            scanf("%d%d",&idx, &dir);
            memset(chk, 0, sizeof(chk));
            dfs(idx-1,dir);
        }
        
        int ans = 0;
        if(m[0][0]) ans += 1;
        if(m[1][0]) ans += 2;
        if(m[2][0]) ans += 4;
        if(m[3][0]) ans += 8;
        
        printf("#%d %d\n",tc, ans);
    } // end of for tc
    
    return 0;
}

Review

  • 오랜만에 다시 풀었는데 문제없이 풀었다.

[2] Answer Code (18. 05. 10)


#include <iostream>
#include <cstring>
using namespace std;

int node[4][8];
int visit[4];

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

void dfs(int idx, int dir){
    if(visit[idx])
        return ;
    
    visit[idx] = 1;
    if(idx+1<4 && node[idx][2] != node[idx+1][6])
        dfs(idx+1,dir * -1);
    if(idx-1>=0 && node[idx][6] != node[idx-1][2])
        dfs(idx-1,dir*-1);
    
    turn(idx,dir);
    visit[idx] = 0;
    
}

int main(){
    int TC;
    cin >> TC;
    
    for(int tc=1; tc<=TC; tc++){
        memset(node,0,sizeof(node));
        memset(visit,0,sizeof(visit));
        
        int k;
        cin >> k;
        
        for(int i=0; i<4; i++){
            for(int j=0; j<8; j++){
                scanf("%d",&node[i][j]);
            }
        }
        
        for(int i=0; i<k; i++){
            int a,b;
            cin >> a >> b;
            dfs(a-1,b);
        }
        
        int ans = 0;
        if(node[0][0] == 1) ans += 1;
        if(node[1][0] == 1) ans += 2;
        if(node[2][0] == 1) ans += 4;
        if(node[3][0] == 1) ans += 8;
        
        printf("#%d %d\n",tc,ans);
    }
    
    
    return 0;
}

Review

  • 30분 걸림

  • 비슷한 문제를 백준에서 풀어서 그런지 확실히 푸는 속도가 빨랐다.


Recommend

Index