0% found this document useful (0 votes)
131 views2 pages

E Mirror Grid

This document contains the code for a problem on Codeforces that involves finding the minimum number of tiles that must be flipped in a square grid to make all rows and columns sum to 0. It defines a custom hash function, type aliases, and input/output functions. The main function reads a test case number and calls the f function. The f function reads a grid, calculates the number of tiles that must be flipped in each layer of the square to make all rows and columns sum to 0, and outputs the total costs.

Uploaded by

Sarievo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views2 pages

E Mirror Grid

This document contains the code for a problem on Codeforces that involves finding the minimum number of tiles that must be flipped in a square grid to make all rows and columns sum to 0. It defines a custom hash function, type aliases, and input/output functions. The main function reads a test case number and calls the f function. The f function reads a grid, calculates the number of tiles that must be flipped in each layer of the square to make all rows and columns sum to 0, and outputs the total costs.

Uploaded by

Sarievo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/**

* Sarievo.
* URL: https://codeforces.com/contest/1703/problem/E
*/

#include <bits/stdc++.h>
using namespace std;

struct custom_hash {
// https://codeforces.com/blog/entry/62393
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM =
chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
template<class t> using hashset=unordered_set<t,custom_hash>;
template<class t,class u> using hashmap=unordered_map<t,u,custom_hash>;
//!<---------- start of define !<--------------------------
using ll=long long;
#define int ll
using ull=unsigned long long;
template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;
using pi=pair<int,int>;
#define mp make_pair
#define a first
#define b second
using vi=vc<int>;
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)x.size()
#define pb push_back
#define eb emplace_back
template<class t,class u> bool chmax(t&a,const u&b){return a<b?a=b,1:0;}
template<class t,class u> bool chmin(t&a,const u&b){return a>b?a=b,1:0;}
template<typename X> inline X abs(const X& a) {return (a<0?-a:a);}
template<typename X> inline X sqr(const X& a) {return (a*a);}
#define FOR(i,a,b) for(int i=int(a);i<int(b);i++) // [a,b)
#define rep(i,b) FOR(i,0,b)
#define DWN(i,a,b) for(int i=int(b)-1;i>=int(a);i--) // (b,a]
#define per(i,b) DWN(i,0,b)
template<class t> void asc(vc<t>& v, int inc=0) {iota(all(v),inc);}
#define acc(a) accumulate(all(a),int(0)) //...
//!<---------- end of define !<----------------------------
#define IOS ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
template<typename T> istream& operator>>(istream& is, vc<T>&v)
{for(auto&e:v)is>>e;return is;}
template<typename T> ostream& operator<<(ostream& os, const vc<T>&v)
{for(auto&e:v)os<<e<<" ";return os;}
template<typename T> istream& operator>>(istream& is, pair<T,T>&v)
{is>>v.a>>v.b;return is;}
template<typename T> ostream& operator<<(ostream& os, const pair<T,T>&v){os<<v.a<<"
"<<v.b;return os;}
//!<---------- end of IO !<-----------------------------
void f();
signed main() {
IOS;
int T=1;
cin >> T;
while (T--) f();
}
//!<---------- end of preset !<----------------------------

void f() {
int n; cin >> n;
vvc<int> g(n, vi(n));
string input;
rep(i, n) {
cin >> input;
rep(j, n) g[i][j] = input[j]-'0';
}

int layers = n/2; // 2->1, 3->1, 4->2..


int costs = 0;
rep(j, layers) {
for (int i = 0; i < n-(j*2)-1; ++i) {
int&d1 = g[j][j+i];
int&d2 = g[j+i][n-1-j];
int&d3 = g[n-1-j][n-1-j-i];
int&d4 = g[n-1-j-i][j];

int ones = d1 + d2 + d3 + d4;


if (ones != 0 && ones != 4) {
if (ones <= 2) {
// d1 = d2 = d3 = d4 = 0;
costs += ones;
} else {
// d1 = d2 = d3 = d4 = 1;
costs += 4-ones;
}
}
}
}
// for(auto&x:g) cout << x << endl;
cout << costs << endl;
}

You might also like