-
Notifications
You must be signed in to change notification settings - Fork 3
/
1284.cpp
42 lines (39 loc) · 1.39 KB
/
1284.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
class Solution {
public:
int minFlips(vector<vector<int>>& mat) {
vector<pair<int, int>> neighbors = {{0, 0}, {1, 0}, {-1, 0}, {0, 1}, {0, -1}};
int m = mat.size();
int n = mat[0].size();
int encode = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
encode |= (mat[i][j] << (i * n + j));
}
}
unordered_set<int> visited;
queue<pair<int, int>> q;
q.push(make_pair(encode, 0));
visited.insert(encode);
while (!q.empty()) {
auto [state, count] = q.front();
q.pop();
if (state == 0) return count;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
int nextState = state;
for (auto neighbor : neighbors) {
int ni = i + neighbor.first;
int nj = j + neighbor.second;
if (ni < 0 || ni >= m || nj < 0 || nj >= n) continue;
nextState ^= 1 << (ni * n + nj);
}
if (visited.find(nextState) == visited.end()) {
q.push(make_pair(nextState, count + 1));
visited.insert(nextState);
}
}
}
}
return -1;
}
};