-
Notifications
You must be signed in to change notification settings - Fork 1
/
1248.cc
99 lines (89 loc) · 1.92 KB
/
1248.cc
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pull;
typedef const ll cll;
typedef queue<ll> qll;
typedef queue<pll> qpll;
typedef priority_queue<ll> pqll;
typedef priority_queue<pll> pqpll;
typedef vector<ll> vll;
cll N = 10;
ll n, mat[N][N] = {{}};
bool search(vll &nums, vll &sums)
{
ll s = nums.size();
if (s == n)
{
for (ll &num : nums)
{
cout << num << " ";
}
return true;
}
sums.emplace_back(0);
for (ll num = -10; num <= 10; ++num)
{
bool isOk = true;
for (ll _sum, sign, i = s; i >= 0; --i)
{
// mat[i-1][s-1]
_sum = sums[i] + num, sign = mat[i][s];
if ((_sum < 0 && sign >= 0) || (_sum > 0 && sign <= 0) || (_sum == 0 && sign != 0))
{
isOk = false;
break;
}
}
if (isOk)
{
nums.emplace_back(num);
for (auto &_sum : sums)
{
_sum += num;
}
if (search(nums, sums))
{
return true;
}
for (auto &_sum : sums)
{
_sum -= num;
}
nums.pop_back();
}
}
sums.pop_back();
return false;
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
for (ll i = 0; i < n; ++i)
{
for (ll l = i; l < n; ++l)
{
char c;
cin >> c;
switch (c)
{
case '+':
mat[i][l] = 1;
break;
case '-':
mat[i][l] = -1;
break;
default:
mat[i][l] = 0;
}
}
}
vll sums, nums;
search(nums, sums);
return 0;
}