-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathLC1284.cpp
More file actions
executable file
·67 lines (57 loc) · 1.63 KB
/
LC1284.cpp
File metadata and controls
executable file
·67 lines (57 loc) · 1.63 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
/*
Problem Statement: https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/
*/
class Solution {
private:
int inf = numeric_limits<int>::max();
public:
int minFlips(vector< vector<int> >& mat) {
int length, states = 1 << (mat.size() * mat[0].size());
vector<int> memo(states, inf);
vector<bool> visited(states);
// Initialize for zero and calculate length
memo[0] = 0;
visited[0] = true;
length = backtrack(memo, visited, mat);
return (length == inf) ? -1 : length;
}
int backtrack(vector<int>& memo, vector<bool>& visited, vector< vector<int> > mat) {
int length, bits = get_binary(mat);
// If state visited, return the calculated length
if (visited[bits])
return memo[bits];
length = inf;
visited[bits] = true;
// Calculate length for state by getting the shortest length
for (int i = 0; i < mat.size(); i++)
for (int j = 0; j < mat[i].size(); j++) {
flip(i, j, mat);
length = min(backtrack(memo, visited, mat), length);
flip(i, j, mat);
}
// To avoid integer overflow
memo[bits] = (length == inf) ? inf : length + 1;
return memo[bits];
}
int get_binary(vector< vector<int> >& mat) {
int bits = 0;
for (int i = 0; i < mat.size(); i++)
for (int j = 0; j < mat[i].size(); j++) {
bits <<= 1;
bits |= mat[i][j];
}
return bits;
}
void flip(int i, int j, vector< vector<int> >& mat) {
// Flip position and all its neighbours
mat[i][j] ^= 1;
if (i > 0)
mat[i - 1][j] ^= 1;
if (i < mat.size() - 1)
mat[i + 1][j] ^= 1;
if (j > 0)
mat[i][j - 1] ^= 1;
if (j < mat[0].size() - 1)
mat[i][j + 1] ^= 1;
}
};