DP라 써놨지만 DFS로 풀었다.
DP의 규칙을 찾지 못해서 DFS로 풀어봤는데 풀렸다.
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int out = 9, n, num;
void DFS(int cnt, int cur)
{
if (cnt > 8)
return;
if (cur == num)
{
out = min(cnt, out);
return;
}
int temp = 0;
for (int i = 0; i + cnt < 9; i++)
{
temp = temp * 10 + n;
DFS(cnt + 1 + i, cur + temp);
DFS(cnt + 1 + i, cur - temp);
DFS(cnt + 1 + i, cur * temp);
DFS(cnt + 1 + i, cur / temp);
}
}
int solution(int N, int number) {
int answer = 0;
n = N; num = number;
DFS(0, 0);
answer = out;
if (answer > 8)
answer = -1;
return answer;
}'Coding_Test 연습 > Programmers' 카테고리의 다른 글
| [프로그래머스] (C++) LV2 N-Queen (0) | 2022.06.11 |
|---|---|
| [프로그래머스] (C++) LV2 하노이의 탑 (0) | 2022.06.11 |
| [프로그래머스] (C++) LV2 줄 서는 방법 (0) | 2022.06.10 |
| [프로그래머스] (C++) LV2 숫자 블록 (0) | 2022.06.10 |
| [프로그래머스] (C++) LV2 멀리 뛰기 (0) | 2022.06.09 |