728x90
https://www.acmicpc.net/problem/10937
현재 위치에서 뒤의 몇 열을 비트 마스크로 관리하는 dp이다. 풀이의 근원은 이 포스트, 또는 이 자료를 참조하라.
이 문제는 위와 달리 칸을 선택하지 않는 경우도 있으므로 이를 고려해서 동적 계획법을 수행하면 된다.
전체 코드
더보기
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
|
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2,fma")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include "bits/stdc++.h"
#include "ext/rope"
using namespace std;
using namespace __gnu_cxx;
using ll = long long;
using pii = pair<int, int>;
int n;
string ar[11];
int dp[11 * 11][1 << 12];
int score[140][140];
int memo(int idx, int mask)
{
if (idx >= n * n - 1) return 0;
int &ret = dp[idx][mask];
if (ret != -1) return ret;
ret = 0;
ret = memo(idx + 1, mask >> 1);
if (mask & 1) return ret = max(ret, memo(idx + 1, mask >> 1));
int val = 0;
if ((mask & 2) == 0 and idx + 1 < n * n)
{
val = score[ar[idx / n][idx % n]][ar[idx / n][idx % n + 1]];
ret = max(ret, val + memo(idx + 2, mask >> 2));
}
if ((mask & (1 << n)) == 0 and idx + n < n * n)
{
val = score[ar[idx / n][idx % n]][ar[idx / n + 1][idx % n]];
int n_mask = mask | (1 << n);
ret = max(ret, val + memo(idx + 1, n_mask >> 1));
}
return ret;
}
void solve()
{
score['A']['A'] = 100;
score['A']['B'] = score['B']['A'] = 70;
score['A']['C'] = score['C']['A'] = 40;
score['B']['B'] = 50;
score['B']['C'] = score['C']['B'] = 30;
score['C']['C'] = 20;
cin >> n;
for (int i = 0; i < n; i++) cin >> ar[i];
memset(dp, -1, sizeof dp);
cout << memo(0, 0);
}
int main()
{
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
cin.tie(nullptr);
ios::sync_with_stdio(false);
solve();
}
|
cs |
제출 기록
728x90
'백준 > DP' 카테고리의 다른 글
백준 1126 - 같은 탑 (0) | 2022.06.13 |
---|---|
백준 1657 - 두부장수 장홍준 (0) | 2022.05.30 |
[KOI] 백준 2673 - 교차하지 않는 원의 현들의 최대집합 (0) | 2022.05.28 |
백준 1937 - 욕심쟁이 판다 (0) | 2021.11.08 |
[ICPC] 백준 11066 - 파일 합치기 (0) | 2021.11.06 |