To solve the problem of making all rows and columns palindromic while ensuring the total number of '1's in the grid is divisible by 4, we can use a dynamic programming (DP) approach.
Explanation:
-
Orbit Identification:
- We start by identifying "orbits" in the grid. An orbit is a set of cells that must be the same to satisfy the palindromic condition for both rows and columns.
- For each cell
(i, j), its orbit includes the cells (i, j), (i, n-1-j), (m-1-i, j), and (m-1-i, n-1-j). The size of an orbit can be 1, 2, or 4 depending on the grid's symmetry.
-
Counting Ones:
- For each orbit, count the number of '1's present in it. This count helps determine the cost of setting the entire orbit to '0' or '1'.
-
Dynamic Programming Setup:
- We use a DP array where
dp[mod] represents the minimum number of flips required to achieve a sum of '1's that is equivalent to mod modulo 4.
- We initialize
dp[0] to 0 (since having zero '1's requires no flips) and the rest of the dp array to infinity.
-
Processing Each Orbit:
- For each orbit, we have two choices: either set all cells in the orbit to '0' or to '1'.
- If we set it to '0', the number of flips required is the number of '1's already present in the orbit.
- If we set it to '1', the number of flips required is the number of '0's present in the orbit.
- We update the DP array based on these choices to keep track of the minimum number of flips required to achieve a sum of '1's that is divisible by 4.
-
Final Calculation:
- After processing all orbits, the value in
dp[0] will give us the minimum number of additional flips needed.
- The total number of flips required is the sum of all '1's in the grid plus the value in
dp[0].
Implementation:
Here is the C++ implementation of the above approach:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int minFlips(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
// Collect all orbits
vector<pair<int, int>> orbits; // Not needed actually
// Instead, store (s,c) for each orbit
vector<pair<int, int>> orbit_info;
int sum_all_c = 0;
int half_m = (m +1)/2;
int half_n = (n +1)/2;
for(int i=0; i < half_m; ++i){
for(int j=0; j < half_n; ++j){
// Collect unique cells in the orbit
vector<pair<int,int>> cells;
cells.emplace_back(i,j);
if(n-1-j !=j){
cells.emplace_back(i, n-1-j);
}
if(m-1-i !=i){
cells.emplace_back(m-1-i, j);
}
if(m-1-i !=i && n-1-j !=j){
cells.emplace_back(m-1-i, n-1-j);
}
// Remove duplicates
sort(cells.begin(), cells.end());
cells.erase(unique(cells.begin(), cells.end()), cells.end());
int s = cells.size();
int c = 0;
for(auto &[x,y]: cells){
c += grid[x][y];
}
orbit_info.emplace_back(s, c);
sum_all_c += c;
}
}
// Initialize DP
// dp[mod] = minimum sum_set(s -c)
// mod: 0,1,2,3
const int INF = 1e9;
vector<int> dp(4, INF);
dp[0] = 0;
for(auto &[s, c]: orbit_info){
vector<int> new_dp(4, INF);
for(int mod=0; mod <4; ++mod){
if(dp[mod] < INF){
// Option1: not select
if(new_dp[mod] > dp[mod]){
new_dp[mod] = dp[mod];
}
// Option2: select
int new_mod = (mod + s) %4;
int new_sum = dp[mod] + (s - c);
if(new_sum < new_dp[new_mod]){
new_dp[new_mod] = new_sum;
}
}
}
dp = move(new_dp);
}
if(dp[0] == INF){
// No solution, though problem states it's always possible
return -1;
}
// Total flips is sum_all_c + dp[0]
return sum_all_c + dp[0];
}
Example Usage:
#include <bits/stdc++.h>
using namespace std;
// The minFlips function as defined above
int main(){
vector<vector<int>> grid1 = {{1,0,0},{0,1,0},{0,0,1}};
cout << minFlips(grid1) << endl; // Output: 3
vector<vector<int>> grid2 = {{0,1},{0,1},{0,0}};
cout << minFlips(grid2) << endl; // Output: 2
vector<vector<int>> grid3 = {{1},{1}};
cout << minFlips(grid3) << endl; // Output: 2
return 0;
}
This implementation correctly identifies the minimal number of flips required to make all rows and columns palindromic while ensuring the total number of '1's in the grid is divisible by 4.