출처: 알고스팟

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
#include <iostream>
#include <vector>
using namespace std;
 
#define INF 987654321
 
vector<vector<int> > switch_list({        // 스위치와 연결된 시계
    vector<int>({ 012 }),
    vector<int>({ 37911 }),
    vector<int>({ 4101415 }),
    vector<int>({ 04567 }),
    vector<int>({ 6781012 }),
    vector<int>({ 021415 }),
    vector<int>({ 31415 }),
    vector<int>({ 4571415 }),
    vector<int>({ 12345 }),
    vector<int>({ 345913 })
    });
 
int min(int a, int b) { return a < b ? a : b; }
 
void SetTheClock(vector<int>& watch, int swtch)
{
    int n = switch_list[swtch].size();
    for (int i = 0; i < n; i++)
    {
        watch[switch_list[swtch][i]] += 3;
        if (watch[switch_list[swtch][i]] > 12)
            watch[switch_list[swtch][i]] = 3;
    }
}
 
int twelveOclock(vector<int>& watch, int swtch)
{
    if (swtch == 10)
    {
        for (int i = 0; i < 16; i++)
            if (watch[i] != 12return INF;
        return 0;
    }
    int ret = INF;
    for (int cnt = 0; cnt < 4; cnt++)
    {
        ret = min(ret, cnt + twelveOclock(watch, swtch+1));    // 시계 돌려돌려
        SetTheClock(watch, swtch);
    }
    return ret;
}
 
int main(void)
{
    int testcase;
    vector<int> watch;
    cin >> testcase;
    while (testcase--)
    {
        for (int i = 0; i < 16; i++)
        {
            int time;
            cin >> time;
            watch.push_back(time);
        }
        int res = twelveOclock(watch, 0);
        if (res == INF) cout << -1 << endl;
        else cout << res << endl;
        watch.clear();
    }
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

알고리즘 200일 프로젝트 - 4day

알고리즘 문제해결 전략 6.8 시계 맞추기(ID:CLOCKSYNC)

난이도 중짜리 문제였지만 내가 생각한 아이디어와 책에서 제시한 풀이법이 같아서 뿌듯했다.

하지만 시계를 4번 움직였을 때 제자리로 돌아온다는 점을 생각은 했으나 문제의 핵심포인트라는 것까지는 생각하지 못하여 원하는 출력결과를 얻지못하였다. 성급하게 구현하는 나쁜습관을 고치도록 노력해야겠다.

또한 아무리 들여봐도 알 수없는 버그때문에 골머리를 앓았는데 그 원인은 허무하게도 첫줄의 벡터배열을 잘못된 방법으로 선언함으로써 발생한 버그였다.

종만북 공부를 잠시 중단하고 effective c++과 뇌를 자극하는 c++ STL 두 권을 먼저 공부 한다음 다시 펼쳐봐야겠다.

내일도 열심히!

+ Recent posts