BOJ 13392 방법을 출력하지 않는 숫자 맞추기

Jmnote (토론 | 기여)님의 2024년 2월 2일 (금) 00:34 판 (새 문서: ==개요== {{BOJ |단계= 47 |분류1= 다이나믹 프로그래밍 }} ==C++== <syntaxhighlight lang='cpp'> #include <bits/stdc++.h> using namespace std; const int MAXN = 10004; int...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요[ | ]

BOJ 13392 방법을 출력하지 않는 숫자 맞추기

2 C++[ | ]

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 10004;
int N;
int arr[MAXN][11];
char curr[MAXN];
char want[MAXN];

int dp(int pos, int offset) {
    if (pos == N) return 0;
    int &result = arr[pos][offset];
    if (result != -1) return result;
    int left = (want[pos] - curr[pos] - offset + 20) % 10;
    int right = 10 - left;
    return result = min(dp(pos + 1, (offset + left) % 10) + left, 
                        dp(pos + 1, offset) + right);
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> N;
    cin >> curr >> want;
    for (int i = 0; i < N; ++i) {
        curr[i] -= '0';
        want[i] -= '0';
    }
    fill(&arr[0][0], &arr[0][0] + sizeof(arr)/sizeof(arr[0][0]), -1);
    cout << dp(0, 0);
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}