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
| #include <bits/stdc++.h>
using namespace std;
int N, ans[10];
char S[10][10];
bool gotAns;
void makeArr(int deep, int beforeSum);
bool check(int deep, int beforeSum);
void goNext(int start, int end, int deep, int bs);
int main() {
cin >> N;
for (int i = 0; i < N; i++)
for (int j = i; j < N; j++)
cin >> S[i][j];
makeArr(0, 0);
for (int i = 0; i < N; i++)
cout << ans[i] << " ";
}
void makeArr(int deep, int beforeSum) {
if (deep == N) {
gotAns = true;
return;
}
switch (S[deep][deep]) {
case '0':
goNext(0, 0, deep, beforeSum);
break;
case '+':
goNext(1, 10, deep, beforeSum);
break;
case '-':
goNext(-10, -1, deep, beforeSum);
break;
}
}
void goNext(int start, int end, int deep, int bs) {
for (int i = start; i <= end; i++) {
if (gotAns)
return;
if (check(deep, bs + i)) {
ans[deep] = i;
makeArr(deep + 1, bs + i);
}
}
}
bool check(int deep, int sum) {
int temp = sum;
for (int i = 0; i < deep; i++) {
if (S[i][deep] == '0' && temp != 0)
return false;
if (S[i][deep] == '+' && temp <= 0)
return false;
if (S[i][deep] == '-' && temp >= 0)
return false;
temp -= ans[i];
}
return true;
}
|