1703E - Mirror Grid - CodeForces Solution


implementation *1200

Please click on ads to support us..

Python Code:

t = int(input())
for i in range(t):
    n = int(input())
    a = []
    op = 0
    for i in range(n):
        a.append(list(map(int, list(input()))))

    for i in range(n - 1):
        for j in range(i, n - 1):
            one = zero = 0

            if a[i][j]:
                one += 1
            else:
                zero += 1
            
            if a[j][n - i - 1]:
                one += 1
            else:
                zero += 1
            
            if a[n - i - 1][n - j - 1]:
                one += 1
            else:
                zero += 1
            
            if a[n - j - 1][i]:
                one += 1
            else:
                zero += 1
            
            if one >= zero:
                op += zero
                a[i][j] = 1
                a[j][n - i - 1] = 1
                a[n - i - 1][n - j - 1] = 1
                a[n - j - 1][i] = 1
            else:
                op += one
                a[i][j] = 0
                a[j][n - i - 1] = 0
                a[n - i - 1][n - j - 1] = 0
                a[n - j - 1][i] = 0
    
    print(op)

C++ Code:

//By TOP
#include <iostream>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <list>
#include <unordered_set>
#include <cmath>
#include <bits/stdc++.h>
#include <deque>
#include <vector>
#include <utility>
using namespace std;
#define int long long
typedef pair<int,int> PII;
#define OO 0x3f3f3f3f
#define rd read()
#define mem(a,b) memset(a,b,sizeof a)
#define ios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define v1 priority_queue<int,vector<int>,greater<int> > v 
#define v2 priority_queue<int,vector<int>,less<int> > v 
#define debug(a) cout << a << "\n"
const int N = 2000010;
int a[N];
int n;
int tr[N];
/*void build(int u , int l , int r)
{
	tr[u] = {l , r};
	if(l == r)
	{
		return ;
	}
	int mid = l + r >> 1;
	build(u << 1 , l , mid);
	build(u << 1 | 1 , mid + 1  ,r);
}*/
/*int query(int u , int l , int r)
{
	if(tr[u].l >= l && tr[u].r <= r)
	{
		return tr[u].v;
	}
	int mid = tr[u].l + tr[u].r >> 1;
	int v = 0;
	if(l <= mid)
	{
		v.query(u << 1 , l , r);
	}
	if(r > mid)
	{
		v.query(u << 1 | 1 , l , r);
	}
	return v;
	
}*/
int lowbit(int x)
{
	return x & -x;
}
void add(int x,int c)
{
	for(int i = x ; i <= n ; i += lowbit(i) )
	{
		tr[i] += c;
	}
}
int query(int x)
{
	int res = 0;
	
	for(int i = x ; i ; i -= lowbit(i))
	{
		res += tr[i];
	}
	return res;
	
}
inline int read()
 {
	int x = 0, f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (ch >= '0' && ch <= '9') {
		x = x * 10 + ch - 48;
		ch = getchar();
	}
	return x * f;
}
char c[1000][1000];

void solve()
{
	int n;
	cin >> n;
	for(int i = 1 ; i <= n ; i ++ )
	{
		for(int j = 1 ; j <= n ; j ++ )
		{
			cin >> c[i][j];
		}
	}
	int ans = 0;
	
	for(int i = 1 ; i <= (n + 1) / 2 ; i ++ )
	{
		for(int j = 1 ; j <= n / 2 ; j ++ )
		{
			int res = 0;
			res += c[i][j] - '0';
			res += c[n + 1 - j][i] - '0';
			res += c[n + 1 - i][n + 1 - j] - '0';
			res += c[j][n + 1 - i] - '0';
			res = min(res , 4 - res);
			ans += res;
			
		}
	}
	cout << ans << "\n";
	
}
signed main()
{
	ios;
	
	int t = 1;
	cin >> t;
	while(t -- )
	{
		solve();
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations
1356. Sort Integers by The Number of 1 Bits
922. Sort Array By Parity II
344. Reverse String
1047. Remove All Adjacent Duplicates In String
977. Squares of a Sorted Array
852. Peak Index in a Mountain Array
461. Hamming Distance
1748. Sum of Unique Elements
897. Increasing Order Search Tree
905. Sort Array By Parity
1351. Count Negative Numbers in a Sorted Matrix
617. Merge Two Binary Trees
1450. Number of Students Doing Homework at a Given Time
700. Search in a Binary Search Tree
590. N-ary Tree Postorder Traversal
589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image