LeetCode 5282. converted into all-zero matrix inversion minimum number of two-way bfs bfs

Address  https://leetcode-cn.com/submissions/detail/39277402/

Title Description
give you a mxn binary matrix mat.

Each step, you can select a cell and its inversion (inversion represents 0 to 1, 1 to 0). If it and the adjacent cell exists, these adjacent cells will be reversed. (Note: two adjacent cells sharing the same side.)

Please return to the matrix mat into a matrix of zeros with the least number of reverse, if not into all-zero matrix, please return -1.

Each grid binary matrix is ​​either 0 or 1.

All-zero matrix is ​​a matrix of all the grid are zero.

Example  1 : 
Input: MAT = [[ 0 , 0 ], [ 0 , 1 ]] 
Output: 3 
Explanation: One possible solution is inverted ( 1 , 0 ) and ( 0 , 1 ) and finally ( 1 , 1 ). 
Example 2 : 

Input: MAT = [[ 0 ]] 
Output: 0 
Explanation: Matrix all-zero matrix is given, so you need to change it. 
Example 3 : 

Input: MAT = [[ . 1 , . 1 , . 1 ], [ . 1 , 0 ,1 ], [ 0 , 0 , 0 ]] 
Output: 6 
Example 4 : 

Input: MAT = [[ 1 , 0 , 0 ], [ 1 , 0 , 0 ]] 
Output: - 1 
Explanation: This matrix can not be changed to all zeros matrix 
 

Tip: 

m ==  mat.length 
n- == MAT [ 0 ] .length
 1 <= m <= . 3 
1 <= n-<= . 3 
MAT [I] [J] is 0 or 1 .

Algorithm 1
this question with acwing 95. inexplicable switch acwing116. Similar pilot brothers

Consider how all zeros when required by the first layer a second layer that is a few switches on how all zeros when needed by a few sequentially push switch to answer the last layer

In the case of large data range can also be used to optimize bidirectional BFS search range

Since the range is relatively small I had taken a relatively rough simple BFS
from the state of all zeros as a starting point in order to see come to BFS title given state requires a few
straightforward
to take notes of key state directly without using two-dimensional arrays compression deformation
, but the code is relatively easy to understand
code is as follows:

 1 class Solution {
 2 public:
 3 
 4 map<vector<vector<int>>, int> visit;
 5 queue<pair<vector<vector<int>>, int>> q;
 6 bool CheckIsAllZero(const vector<vector<int>> &mat)
 7 {
 8     for (int i = 0; i < mat.size(); i++) {
 9         for (int j = 0; j < mat[0].size(); j++) {
10             if (mat[i][j] != 0)
11                 return false;
12         }
13     }
14 
15     return true;
16 }
17 
18 
19 void Click(vector<vector<int>>& currenrState, int x, int y)
20 {
21     int addx[4] = { 1,-1,0,0 };
22     int addy[4] = { 0,0,-1,1 };
23 
24     currenrState[x][y] = currenrState[x][y] ? 0: 1;
25 
26     for (int i = 0; i < 4; i++) {
27         int newx = x + addx[i];
28         int newy = y + addy[i];
29 
30         if (newx >= 0 && newx < currenrState.size() && newy >= 0 && newy < currenrState[0].size()) {
31             currenrState[newx][newy] = currenrState[newx][newy] ? 0 : 1;
32         }
33     }
34 }
35 
36 
37 int minFlips(vector<vector<int>>& mat) {
38     if (CheckIsAllZero(mat)) return 0;
39 
40     vector<vector<int>> matAllZero(mat.size(), vector<int>(mat[0].size()));
41 
42     int distance = 0;
43 
44     visit[matAllZero] = distance;
45     q.push({ matAllZero ,distance });
46 
47     while (!q.empty()) {
48         auto qe = q.front();
49         q.pop();
50         vector<vector<int>> currenrState = qe.first;
51         int currentCount = qe.second;
52 
53         //尝试 点击该XY
54         for (int i = 0; i < currenrState.size(); i++) {
55             for (int j = 0; j < currenrState[0].size(); j++) {
56                 vector<vector<int>> copy = currenrState;
57                 Click(copy, i, j);
58 
59 
60                 if (copy == mat)
61                 {
62                     return currentCount + 1;
63                 }
64 
65                 if (visit.count(copy) == 0) {
66                     q.push({ copy ,currentCount + 1 });
67                     visit[copy] = currentCount + 1;
68                 }
69             }
70         }
71     }
72 
73 
74     return -1;
75 }
76 
77 };
View Code

 

Guess you like

Origin www.cnblogs.com/itdef/p/12005600.html
Recommended