-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtilepaint_BF.cpp
More file actions
135 lines (108 loc) · 3.1 KB
/
tilepaint_BF.cpp
File metadata and controls
135 lines (108 loc) · 3.1 KB
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 <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
typedef long long ll;
typedef vector< ll > vl;
typedef vector< vector<ll> > vll;
typedef vector< vector< pair<ll, bool> > > vlb;
typedef vector< vector<bool> > vbb;
ll N, M;
auto timeStart = high_resolution_clock::now();
bool checkSum(vl CC, vl CR) {
ll sum_row = 0, sum_column = 0;
for (ll item : CC) {
if (item == -1) return true;
sum_column += item;
}
for (ll item : CR) {
if (item == -1) return true;
sum_row += item;
}
return sum_column == sum_row;
}
bool checkIfMatch(vlb &cell, vl CC, vl CR) {
vl coloredCol = vl(M, 0), coloredRow = vl(N, 0);
for (ll i = 0; i < N; i++)
for (ll j = 0; j < M; j++) {
if (cell[i][j].second) {
coloredCol[j]++;
coloredRow[i]++;
}
}
for (ll j = 0; j < M; j++) {
if (CC[j] != -1 && coloredCol[j] != CC[j]) {
return false;
}
}
for (ll j = 0; j < N; j++) {
if (CR[j] != -1 && coloredRow[j] != CR[j]) {
return false;
}
}
return true;
}
vll solution;
void color(vlb &cell, int tile, int val) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
if (cell[i][j].first == tile) {
cell[i][j].second = val;
}
}
}
}
void findPossiblePaint(vlb &cell, ll N, vl CC, vl CR) {
// BRUTE FORCE
ll nilai = (1 << N);
for (ll bitmask = 0; bitmask <= nilai; bitmask++) {
if (duration_cast<milliseconds>(high_resolution_clock::now() - timeStart).count() > 600000) {
cout << "TOO SLOW" << endl;
return;
}
for (int k = 0; k < N; k++) {
if (((1 << k) & bitmask) > 0) {
color(cell, k+1, 1);
}
}
if (checkIfMatch(cell, CC, CR)) {
cout << "FOUND VALID COMBINATION! : " << endl;
for (int k = 0; k < N; k++) {
if (((1 << k) & bitmask) > 0) {
cout << k + 1 << " ";
}
}
cout << endl;
return;
}
for (int k = 0; k < N; k++) {
if (((1 << k) & bitmask) > 0) {
color(cell, k+1, 0);
}
}
}
cout << "NO SOLUTION" << endl;
}
int main() {
cin >> N >> M;
vl CR(N), CC(M);
for (int i = 0; i < M; i++) {
cin >> CC[i];
}
for (int i = 0; i < N; i++) {
cin >> CR[i];
}
ll t = 0;
vlb cell(N, (vector< pair<ll, bool> >(M, {0, 0})));
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
cin >> cell[i][j].first;
t = max(t, cell[i][j].first);
}
}
if (N >= 12 && M >= 12) { cout << "too large"; return 0; }
timeStart = high_resolution_clock::now();
findPossiblePaint(cell, t, CC, CR);
auto timeEnd = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(timeEnd - timeStart);
cout << "Execution Time: " << duration.count() << "ms." << endl;
}