961C - Chessboard - CodeForces Solution


bitmasks brute force implementation *1400

Please click on ads to support us..

Python Code:

n=int(input())
change_num1=[]
change_num2=[]

valid_line1="01"*(n//2)+"0"
valid_line2="10"*(n//2)+"1"
for k in range(4):
    t1=0
    t2=0
    for i in range(n):
        line=input()
        for j in range(n):
            if line[j]!=valid_line1[j]:
                t1+=1
            if line[j]!=valid_line2[j]:
                t2+=1
        valid_line1,valid_line2=valid_line2,valid_line1
    valid_line1,valid_line2=valid_line2,valid_line1
    if k!=3:
        input()
    change_num1.append(t1)
    change_num2.append(t2)

minact=4*n*n
for l1 in range(4):
    for l2 in range(l1+1,4):
        i=2**l1+2**l2
        act=0
        for j in range(4):
            if i%2==1:
                act+=change_num1[j]
            else:
                act+=change_num2[j]
            i//=2
        if act<minact:
            minact=act
print(minact)

  			   	  		     			   	 		 		

C++ Code:

#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <utility>
#include <string>
#include <cmath>
#include <numeric>
#include <iomanip>
#include <algorithm>
#define ll long long
#define ar array
#define pb push_back
#define sc static_cast
#define ff first
#define ss second
// for pbds or policy based data structures:
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace __gnu_pbds;
#define pbds tree<ll, null_type,less<ll>, rb_tree_tag,tree_order_statistics_node_update>

#define pbds_ms tree<ll, null_type,less_equal<ll>, rb_tree_tag,tree_order_statistics_node_update>

#define pbds_g tree<ll, null_type,greater<ll>, rb_tree_tag,tree_order_statistics_node_update>

//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
//typedef tree<int,null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> pbds;
const ll mod=1e9+7;
using namespace std;

ll gcd(ll a, ll b){
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
//
//bool prime(in n)
//{
//    for(in i=2;i*i<=n;i++)
//    if(n%i==0)
//    return 0;
//    return 1;
//}
//
//vector<vector<ll> > subsetscomb(vector<ll>& v, ll n){
//	vector<vector<ll> > subsets;
//	for(ll i=0; i< (1<<n); i++){
//		vector<ll> f;
//		ll o = i;
//		for(ll j=0; j<n; j++){
//			if(o&(1<<j)){
//				f.push_back(v[j]);
//			}
//		}
//		subsets.push_back(f);
//	}
//	ll k = subsets.size();
//	cout<<"SUBSETS: \n";
//	cout<<"size: "<<k<<"\n";
//	for(ll i=0; i<k; i++){
//		ll m = subsets[i].size();
//		for(ll j=0; j<m; j++){
//			cout<<subsets[i][j]<< " ";
//		}cout<<"\n";
//	}
//	cout<<" ------------------- \n";
//	return subsets;
//}

//ll counter(ll x){
//	ll c =0;
//	for(ll i=2; i<=x; i++){
//		if(x%i==0){
//			c++;
//		}
//	}return c;
//}

//ll lcm(ll a, ll b)
//{
//    return (a / gcd(a, b)) * b;
//}


//void dfs(vector<vector<ll> > &g, vector<ll> &vis, vector<ll> &p, ll idx){
//	vis[idx] = 1;
//	ll k = g[idx].size();
//	for(ll i=0; i<k; i++){
//		ll id = g[idx][i];
//		if(vis[id]==0){
//			p[id] = idx;
//			dfs(g,vis,p,id);
//		}
//	}
//	return;
//}


//void dfs(vector<vector<ll> > &g, vector<vector<ll> > &vis, ll i, ll j, vector<pair<ll,ll> > &a1){
//	vis[i][j] = 1;
//	ll n = g.size();
//	a1.push_back({i,j});
//	vector<ll> x = {0,0,1,-1};
//	vector<ll> y = {-1,1,0,0};
//	for(ll q=0; q<4; q++){
//		ll xx = i+x[q];
//		ll yy = j+y[q];
//		if(xx>=0 && yy>=0 && xx<n && yy<n && vis[xx][yy]==0 && g[xx][yy]==0){
//			dfs(g,vis,xx,yy,a1);
//		}
//	}
//	return;
//}


void dfs(vector<vector<vector<ll> > > &g, vector<vector<ll> > &vis, ll i, ll j){
	vis[i][j] = 1;
	for(ll q=0; q<g[i][j].size(); q++){
		if(g[i][j][q]==1 && vis[i][j+1]==0){
			dfs(g,vis,i,j+1);
		}
		if(g[i][j][q]==-1 && vis[i][j-1]==0){
			dfs(g,vis,i,j-1);
		}
		if(g[i][j][q]==2 && vis[i+1][j]==0){
			dfs(g,vis,i+1,j);
		}
		if(g[i][j][q]==-2 && vis[i-1][j]==0){
			dfs(g,vis,i-1,j);
		}
	}
	return;
}


void changes(vector<vector<ll> > &v1, ll &b, ll x){
	ll n = v1.size();
	for(ll i=0; i<n; i++){
		if(x==0){
			x = 1;
		}else{
			x = 0;
		}
		for(ll j=0; j<n; j++){
			if(j%2==0){
				if(v1[i][j]!=x){
					b++;
				}
			}else{
				if(v1[i][j]==x){
					b++;
				}
			}
		}
	}
	return;
}


void solve() {
	ll n;
	cin>>n;
	string f;
	getline(cin,f);
	vector<vector<ll> > v1;
	vector<vector<ll> > v2;
	vector<vector<ll> > v3;
	vector<vector<ll> > v4;
	for(ll i=0; i<n; i++){
		vector<ll> r;
		for(ll j=0; j<n; j++){
			r.push_back(0);
		}
		v1.push_back(r);
		v2.push_back(r);
		v3.push_back(r);
		v4.push_back(r);
	}
	
	for(ll i=0; i<n; i++){
		string s;
		getline(cin,s);
		for(ll j=0; j<n; j++){
			ll k = s[j]-'0';
			v1[i][j] = k;
		}
	}
	string t1;
	getline(cin,t1);
	
	for(ll i=0; i<n; i++){
		string s;
		getline(cin,s);
		for(ll j=0; j<n; j++){
			ll k = s[j]-'0';
			v2[i][j] = k;
		}
	}
	string t2;
	getline(cin,t2);
	
	
	for(ll i=0; i<n; i++){
		string s;
		getline(cin,s);
		for(ll j=0; j<n; j++){
			ll k = s[j]-'0';
			v3[i][j] = k;
		}
	}
	string t3;
	getline(cin,t3);
	
	
	for(ll i=0; i<n; i++){
		string s;
		getline(cin,s);
		for(ll j=0; j<n; j++){
			ll k = s[j]-'0';
			v4[i][j] = k;
		}
	}
	
	ll b1 = 0;
	ll w1 = 0;
	ll b2 = 0;
	ll w2 = 0;
	ll b3 = 0;
	ll w3 = 0;
	ll b4 = 0;
	ll w4 = 0;
	
	changes(v1,b1,1);
	changes(v1,w1,0);
	changes(v2,b2,1);
	changes(v2,w2,0);
	changes(v3,b3,1);
	changes(v3,w3,0);
	changes(v4,b4,1);
	changes(v4,w4,0);
	ll k1 = b1+b2+w3+w4;
	ll k2 = b1+b3+w2+w4;
	ll k3 = b1+b4+w2+w3;
	ll k4 = b2+b3+w1+w4;
	ll k5 = b2+b4+w1+w3;
	ll k6 = b3+b4+w1+w2;
	ll mm = fmin(k1,k2);
	ll mn = fmin(k3,k4);
	ll nn = fmin(k5,k6);
	ll ans = fmin(fmin(mm,mn),nn);
	cout<<ans<<"\n";
	
	
	return;
}




int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	solve();
//	ll t;
//	cin>>t;
////	string s;
////	getline(cin,s);
//	while(t--){
//		solve();
//	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses