[BOJ] 2629번 양팔저울 - C++

"동적 계획법2"

Posted by Yongmin on July 12, 2021

문제

양팔저울

풀이

Reference를 참조하였다.

소스 코드

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
#include<iostream>
#include<cmath>
 
#define endl "\n"
#define MAX 30
using namespace std;
 
int N, M;
int Weight[MAX];
int Find[7];
bool Visit[MAX + 1][MAX * 500 + 1];
 
void Input()
{
    cin >> N;
    for (int i = 0; i < N; i++)
    {
        cin >> Weight[i];
    }
    cin >> M;
    for (int i = 0; i < M; i++)
    {
        cin >> Find[i];
    }
}
 
void Make_Weight(int Cnt, int Result)
{
    if (Cnt > N) return;
    if (Visit[Cnt][Result] == true) return;
 
    Visit[Cnt][Result] = true;
 
    Make_Weight(Cnt + 1, Result + Weight[Cnt]);
    Make_Weight(Cnt + 1, Result);
    Make_Weight(Cnt + 1, abs(Result - Weight[Cnt]));
}
 
void Solution()
{
    Make_Weight(0, 0);
    for (int i = 0; i < M; i++)
    {
        if (Find[i] > MAX * 500) cout << "N ";
        else if (Visit[N][Find[i]] == true) cout << "Y ";
        else cout << "N ";
    }
    cout << endl;
}
 
void Solve()
{
    Input();
    Solution();
}
 
int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
    //freopen("Input.txt", "r", stdin);
    Solve();
 
    return 0;
}


//출처: https://yabmoons.tistory.com/105 [얍문's Coding World..]


# # #