-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp11002 - Towards Zero.cpp
135 lines (109 loc) Β· 4.38 KB
/
p11002 - Towards Zero.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <cstring>
constexpr int MAX_SUM = 100;
using namespace std;
int board[70][70] = { 0 };
bool sum_pos[70][70][MAX_SUM];
bool sum_neg[70][70][MAX_SUM];
void insert(int row, int col, int value) {
if (value >= 0)
sum_pos[row][col][value] = true;
else
sum_neg[row][col][-value] = true;
}
int main() {
ios::sync_with_stdio(false);
int rows = 0;
cin >> rows;
while (rows != 0) {
memset(board, -1, sizeof board);
memset(sum_pos, false, sizeof sum_pos);
memset(sum_neg, false, sizeof sum_neg);
int input = 0;
for (int i = 1; i <= rows * 2 - 1; i++) {
int limit = i <= rows ? i : rows * 2 - i;
for (int j = 1; j <= limit; j++) {
cin >> input;
board[i][j] = abs(input);
}
}
insert(2 * rows - 1, 1, board[2 * rows - 1][1]);
insert(2 * rows - 1, 1, -board[2 * rows - 1][1]);
for (int i = rows * 2 - 1; i >= 2; i--) {
if (i > rows) {
for (int j = 1; j <= rows * 2 - i; j++) {
for (int k = 0; k < MAX_SUM; k++) {
if (!sum_pos[i][j][k] && !sum_neg[i][j][k])
continue;
if (sum_pos[i][j][k]) {
if (board[i - 1][j] != -1) {
// left
insert(i - 1, j, k + board[i - 1][j]);
insert(i - 1, j, k - board[i - 1][j]);
}
if (board[i - 1][j + 1] != -1) {
// right
insert(i - 1, j + 1, k + board[i - 1][j + 1]);
insert(i - 1, j + 1, k - board[i - 1][j + 1]);
}
}
if (sum_neg[i][j][k]) {
if (board[i - 1][j] != -1) {
// left
insert(i - 1, j, -k + board[i - 1][j]);
insert(i - 1, j, -k - board[i - 1][j]);
}
if (board[i - 1][j + 1] != -1) {
// right
insert(i - 1, j + 1, -k + board[i - 1][j + 1]);
insert(i - 1, j + 1, -k - board[i - 1][j + 1]);
}
}
}
}
}
else {
for (int j = 1; j <= i; j++) {
for (int k = 0; k < MAX_SUM; k++) {
if (!sum_pos[i][j][k] && !sum_neg[i][j][k])
continue;
if (sum_pos[i][j][k]) {
if (board[i - 1][j - 1] != -1) {
// left
insert(i - 1, j - 1, k + board[i - 1][j - 1]);
insert(i - 1, j - 1, k - board[i - 1][j - 1]);
}
if (board[i - 1][j] != -1) {
// right
insert(i - 1, j, k + board[i - 1][j]);
insert(i - 1, j, k - board[i - 1][j]);
}
}
if(sum_neg[i][j][k]) {
if (board[i - 1][j - 1] != -1) {
// left
insert(i - 1, j - 1, -k + board[i - 1][j - 1]);
insert(i - 1, j - 1, -k - board[i - 1][j - 1]);
}
if (board[i - 1][j] != -1) {
// right
insert(i - 1, j, -k + board[i - 1][j]);
insert(i - 1, j, -k - board[i - 1][j]);
}
}
}
}
}
}
int min = MAX_SUM;
for (int i = 0; i < MAX_SUM; i++) {
if (sum_pos[1][1][i] || sum_neg[1][1][i]) {
min = i;
break;
}
}
cout << min << endl;
cin >> rows;
}
return 0;
}