본문 바로가기
SWEA

[SWEA] 4013 특이한 자석 (모의 SW 역량테스트)

by 콩순이냉장고 2020. 9. 27.

문제 URL : swexpertacademy.com/main/solvingProblem/solvingProblem.do

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

문제접근법 : 자석은 4개 입니다. 회전을위해서 편리하게 DEQUE를 이용하면 됩니다.

시계방향이든 반시계방향이든 디큐로 2줄로 쉽게 회전이 가능합니다.

그리고 왼쪽의 마주보는 것과 다른극일때 회전을 해야하고 그 또 왼쪽에 자석과도 다를경우 그것도 회전해야합니다. 그치만 한꺼번에 회전을하는거니 맨왼쪽을 먼저 회전한후 마지막은 자신이 회전시키면 됩니다.

오른쪽도 왼쪽하는방법과 마찬가지입니다. 이것만 알면 구현은 간단합니다.

 

소스코드 : 

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <deque>
 
using namespace std;
 
deque<int> magnet[6];
int k;
vector<pair<intint>> rv;
int Count(int n)// 맨위 카운트세키
{
    n--;
    return 1 << n;
}
void input(){
    cin >> k;
    for (int i = 1; i <= 4; i++)
    {
        magnet[i].clear();
        magnet[i].resize(8);
        for (int j = 0; j < 8; j++)
            cin >> magnet[i][j];
    }
    rv.clear();
    rv.resize(k);
    for (int i = 0; i < k; i++)
        cin >> rv[i].first >> rv[i].second;
 
}
void rotate(int idx, int dir){
    if (dir>0)
    {
        magnet[idx].push_front(magnet[idx].back());
        magnet[idx].pop_back();
    }
    else
    {
        magnet[idx].push_back(magnet[idx].front());
        magnet[idx].pop_front();
    }
}
void lsearch(int idx, int dir)
{
    if (idx < 1)
        return;
    if (magnet[idx][6!= magnet[idx - 1][2]){
        lsearch(idx - 1, dir*-1);
        rotate(idx - 1, dir*-1);
    }
}
void rsearch(int idx, int dir)
{
    if (idx>4)
        return;
    if (magnet[idx][2!= magnet[idx + 1][6]){
        rsearch(idx + 1, dir*-1);
        rotate(idx + 1, dir*-1);
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    vector<int> v;
    v.resize(10);
    int te;
    cin >> te;
    magnet[0].resize(8);//0과 5번 1~4번은 자석이 있지만 1번 왼쪽을 볼때는 없지만 그냥 있는것처럼보고 완전히다른것으로 표현
    magnet[5].resize(8);
    for (int i = 0; i < 8; i++){
        magnet[0][i] = -1;
        magnet[5][i] = -1;
    }
 
    for (int test = 1; test <= te; test++)
    {
        input();
        int sum = 0;
        for (int i = 0; i < k; i++)
        {
            lsearch(rv[i].first, rv[i].second);//왼쪽회전
            rsearch(rv[i].first, rv[i].second);//오른쪽회전
            rotate(rv[i].first, rv[i].second);//자기자신 회전
        }
        for (int i = 1; i <= 4; i++)
        {
            if (magnet[i][0])
                sum += Count(i);
        }
        cout << "#" << test << " " << sum << "\n";
    }
}
cs

모르는점 혹은 궁금한점 혹은 논리적인 오류같은것이라도 있다면 언제든지 댓글을 이용해주시길 바랍니다.