๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/2629
ํ์ด
๋ฐฑํธ๋ํน์ ๊ณต๋ถํ๋ค๊ฐ ๋ง๋ ๋ฌธ์ ๋ค. ์ ์ธ์ ๊ธฐ์ค์ผ๋ก ์๊ฐํ์ ๋ i๋ฒ์งธ ์ถ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ์ 3๊ฐ์ง๊ฐ ์๋ค.
1. ์ผ์ชฝ์ ๋๋๋ค
2. ์ค๋ฅธ์ชฝ์ ๋๋๋ค
3. ๋์ง ์๋๋ค
๋งจ ์ฒ์ ๋ ์ค๋ฅธ ์์ด๋์ด๊ฐ ์ด๊ฒ์ด์์ผ๋ฏ๋ก ์ด๊ฒ์ ๊ธฐ๋ฐ์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ๋ค.
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
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <climits>
#include <algorithm>
#include <tuple>
#include <cmath>
using namespace std;
typedef long long ll;
const int MAX_N = 40000 + 5;
int is_ok[MAX_N];
int N, T;
vector<int> weight;
void backtracking(int depth, int Left, int Right) {
if (is_ok[abs(Left - Right)])
return;
is_ok[abs(Left - Right)] = 1;
if (depth == N) {
return;
}
backtracking(depth + 1, Left + weight[depth], Right); //์ถ๋ฅผ ์ผ์ชฝ์ ์ฌ๋ฆฌ๊ฑฐ๋
backtracking(depth + 1, Left, Right + weight[depth]); //์ถ๋ฅผ ์ค๋ฅธ์ชฝ์ ์ฌ๋ฆฌ๊ฑฐ๋
backtracking(depth + 1, Left, Right); //์ถ๋ฅผ ์์ฌ๋ฆฌ๊ฑฐ๋
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
int num;
for (int i = 0; i < N; i++) {
cin >> num;
weight.push_back(num);
}
cin >> T;
backtracking(0, 0, 0);
while (T--) {
cin >> num;
if (is_ok[num])
cout << "Y";
else cout << "N";
cout << " ";
}
return 0;
}
|
cs |
์ด๋ ๊ฒ ์ธ ๊ฐ๋์ ๋ฐฑํธ๋ํน์ ์งํํ ๊ฒฐ๊ณผ. ์๊ฐ์ด๊ณผ๋ฅผ ๋ง์ดํ๋ค. ์๊ฐํด๋ณด๋ฉด ์ด ์ฝ๋์ ์๊ฐ๋ณต์ก๋๋
3์ N์ ๊ณฑ. N์ด ์ต๋ 30์ด๋๊น ์ต์ ์ ๊ฒฝ์ฐ 3์ 30์ ๊ณฑ. ์๊ฐ์ ํ์ 1์ด๋ฐ์ ์๋๋๋ฐ 3์30์ ๊ณฑ์ 10์ต์
ํ์ฉ ๋ฐ์ด๋๋ ์ซ์๋ค. ์๊ฐ์ ์ค์ด๊ธฐ ์ํด ํ์ฐธ์ ๊ณ ๋ฏผํ๋ค. 3์ 10์ ๊ณฑ์ 5๋ง ๋จ์งํ๋๊น 30์ด๋ฉด 10๊ฐ์ฉ 3๊ฐ๋ก ๋๋ ์ ๊ณ์ฐํ๋ฉด ๋์ง์์๊น? ํ๋๋ฐ ์ฝ๋๋ก ๊ตฌํํ๊ธฐ๊ฐ ๋ถ๊ฐ๋ฅํ๊ฑด์ง ์ด๋ ค์ ๋ค. ๊ฒฐ๊ตญ ๊ณ ๋ฏผ๋์ ๋ค๋ฅธ๋ถ์ ์ฝ๋๋ฅผ ์ฐธ๊ณ ํ๊ธฐ๋ก ํ๋ค.
์ฐธ๊ณ ํ ์ฝ๋
https://cocoon1787.tistory.com/360
์ด ๋ถ์ ์ฝ๋๋ฅผ ๋ณด๋ฉด ๋งค์ปค๋์ฆ์ ๋์ ๋น์ทํ๋ค. ์ฐจ์ด๊ฐ ์๋ค๋ฉด
1
|
if (i > n || dp[i][w]) return;
|
cs |
์ด ๋ถ๋ถ๋ง ๋ฌ๋๋ค. ๋๋ is_ok๋ผ๋ 1์ฐจ์ ๋ฐฐ์ด์ ๊ทธ ๊ตฌ์ฌ์ ๋ฌด๊ฒ๊ฐ ์ธก์ ๊ฐ๋ฅํ์ง ์ฌ๋ถ๋ฅผ ์ ์ฅํ๊ณ ์์๊ณ ์ ๋ถ์ 2์ฐจ์ ๋ฐฐ์ด์ ๋ง๋ค์ด i๊ฐ ๊น์ง ๊ฒ์ฌํ์๋ w์ ๋ฌด๊ฒ ๊ตฌ์ฌ์ ์ธก์ ๊ฐ๋ฅํ์ง ์ฌ๋ถ๋ฅผ ์ ์ฅํ๋ค.
๊ทธ๋ฆฌ๊ณ dp[i][w]๊ฐ ๊ฐ๋ฅํ๋ค๊ณ ํ๋ณ๋ฌ์ผ๋ฉด returnํด์ ์ค๋ณต์ ๋ฐฉ์งํ๋ค. ์ด ๋ถ๋ถ์ ์ฐธ๊ณ ํด์ ๋ด ์ฝ๋๋ฅผ ์์ ํ๋ค.
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
|
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string.h>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <climits>
#include <algorithm>
#include <tuple>
#include <cmath>
using namespace std;
typedef long long ll;
const int MAX_N = 40000 + 5;
bool is_ok[31][MAX_N];
int N, T;
vector<int> weight;
void backtracking(int depth, int Left, int Right) {
if (is_ok[depth][abs(Left - Right)])
return;
is_ok[depth][abs(Left - Right)] = 1;
if (depth == N)
return;
backtracking(depth + 1, Left + weight[depth], Right); //์ถ๋ฅผ ์ผ์ชฝ์ ์ฌ๋ฆฌ๊ฑฐ๋
backtracking(depth + 1, Left, Right + weight[depth]); //์ถ๋ฅผ ์ค๋ฅธ์ชฝ์ ์ฌ๋ฆฌ๊ฑฐ๋
backtracking(depth + 1, Left, Right); //์ถ๋ฅผ ์์ฌ๋ฆฌ๊ฑฐ๋
}
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N;
int num;
for (int i = 0; i < N; i++) {
cin >> num;
weight.push_back(num);
}
cin >> T;
backtracking(0, 0, 0);
while (T--) {
cin >> num;
if (is_ok[N][num])
cout << "Y";
else cout << "N";
cout << " ";
}
return 0;
}
|
cs |
๊ทธ๋ ๊ฒ ์ ์ถํ ๊ฒฐ๊ณผ ์๊ฐ์ด๊ณผ๋ฅผ ํผํ ์ ์์๋ค. ๋ ์์ ์ ์ฅ๋ฐฉ์์ ํ์ฉํด์ ์๊ฐ๋ณต์ก๋๋ฅผ ๋ฎ์ถ ์ผ์ด์ค์๋ค.
์ ์ฅํ๋ ๋ฐฉ๋ฒ์ ์ค์ ํ ๋ ์ ์คํด์ผ ํ๋ค๋ ๊ฒ์ ์๊ฒํด ์ค ๋ฌธ์ ๋ค.
'๋ฐฑ์ค > Gold' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ] 1707 : ์ด๋ถ ๊ทธ๋ํ (0) | 2022.01.21 |
---|---|
[BOJ] 1579 : ์ฑ (0) | 2022.01.20 |
[BOJ] 10942 : ํฐ๋ฆฐ๋๋กฌ? (0) | 2022.01.20 |
[BOJ] 2661: ์ข์ ์์ด (0) | 2022.01.19 |
[BOJ] 1799 : ๋น์ (0) | 2022.01.19 |