-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsame_consec_diff.cpp
59 lines (47 loc) · 1.18 KB
/
same_consec_diff.cpp
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
#include <iostream>
#include <vector>
using namespace std;
void DFS(int N, int num, int K, vector<int>& results) {
if (N == 0) {
results.push_back(num);
return;
}
vector<int> nextDigits;
int tailDigit = num % 10;
nextDigits.push_back(tailDigit + K);
if (K != 0) {
nextDigits.push_back(tailDigit - K);
}
for (int nextDigit : nextDigits) {
if (0 <= nextDigit && nextDigit < 10) {
int newNum = num * 10 + nextDigit;
DFS(N - 1, newNum, K, results);
}
}
}
vector<int> numsSameConsecDiff(int N, int K) {
vector<int> results;
if (N == 1) {
for (int i = 0; i <= 9; i++) {
results.push_back(i);
}
return results;
}
for (int num = 1; num <= 9; num++) {
DFS(N - 1, num, K, results);
}
return results;
}
int main() {
int N, K;
cin >> N >> K;
vector<int> results = numsSameConsecDiff(N, K);
for (int i = 0; i < results.size(); i++) {
if (i != results.size() - 1) {
cout << results[i] << " ";
} else {
cout << results[i];
}
}
return 0;
}