zoj3838浙大月赛 大模拟

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3838

Bob is recently playing a game called Minecraft, especially a mod called Thaumcraft. It is a mod of magic.

Usually, Bob has Obsessions with Symmetry while playing Minecraft. This obsession is useless in the gameplay generally. However, in Thaumcraft, the infusion altar requires symmetry to keep it stable.

Bob built an infusion altar in his secret chamber, but it was not so symmetrical. After some explosions, Bob decided to fix the infusion altar to make it symmetrical.

zoj3838浙大月赛 大模拟_第1张图片

You will be given the map of Bob's secret chamber. It is of size n*n(n is an odd number), the infusion altar is always at the center of his secret chamber. The following picture is a typical map. The 3*3 square in the center is the Infusion Altar, it is a multi-block structure. Here, '#' means Runic Matrix, 'o' means Arcane Pedestal, '.' means an empty place, 'a'-'z' means occult paraphernalia(like skulls, crystals and candles) Bob placed around the Infusion Altar. There will not be characters other than 'a'-'z', '.', '#'.

.aab.
bo.ob
b.#.a
bo.ob
bbab.

Now, the question is that at least how many blocks need to be changed to make the whole map symmetrical. Here, being symmetrical means having all four axes of symmetry for a square. Also, you can change any character on the map to any other character.

Input

There are multiple cases. The first line contains one integer T which is the number of test cases.
For each case, The first line contains an integer n ( 3 ≤ n ≤ 99, and n is an odd number)
For the next n lines, each line contains n characters showing the map.
It is guaranteed that the Infusion Altar is at the center of the map. 
It is guaranteed that only 'a'-'z' and '.' will appear out of the Infusion Altar.

Output

One integer for each test case which is the least number of blocks that should be changed.

Sample Input

3
3
o.o
.#.
o.o
5
.aab.
bo.ob
b.#.a
bo.ob
bbab.
5
aabba
ao.oa
a.#.a
ao.oa
aaaaa

Sample Output

0
3
2

Hint

The first sample is a standard Infusion Altar.
In second sample, Bob will change his secret chamber to the following map.

.bab.
bo.ob
a.#.a
bo.ob
.bab.
/**
题意:给你一个n*n的棋盘每个棋子都由特定的字母组成,求最少移动多少步使得这个棋盘里的棋子关于横轴纵轴对角线都对称
解题思路:四个轴将棋盘平均分成了8份,我们枚举其中一份,根据对称的原则表示出另外七份,然后把这八份都变成原来占份数最多的字母。在枚举四条轴上的字母即可
(i,j)(i,n-1-j)(n-1-i,j)(n-1-i,n-1-j)(j,i)(j,n-1-i)(n-1-j,i)(n-j-1,n-1-i)
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
char a[102][102];
int n,maxx;
int main()
{
    int T;
    cin >> T;
    while(T--)
    {
        cin >> n;
        for(int i=0; i<n; i++)
            cin >> a[i];
        int sum=0;
        for(int i=0; i<n/2; i++)
        {
            for(int j=i+1; j<n/2; j++)
            {
                maxx=1;
                map<char,int>num;
                num[a[i][j]]++;
                maxx=max(num[a[i][j]],maxx);
                num[a[i][n-1-j]]++;
                maxx=max(num[a[i][n-1-j]],maxx);
                num[a[n-i-1][j]]++;
                maxx=max(num[a[n-i-1][j]],maxx);
                num[a[n-1-i][n-1-j]]++;
                maxx=max(num[a[n-1-i][n-1-j]],maxx);
                num[a[j][i]]++;
                maxx=max(num[a[j][i]],maxx);
                num[a[n-1-j][i]]++;
                maxx=max(num[a[n-1-j][i]],maxx);
                num[a[j][n-i-1]]++;
                maxx=max(num[a[j][n-i-1]],maxx);
                num[a[n-1-j][n-1-i]]++;
                maxx=max(num[a[n-1-j][n-1-i]],maxx);
                sum+=(8-maxx);
            }
        }
        for(int i=0; i<n/2; i++)
        {
            int m=n/2;
            maxx=1;
            map<char,int>num;
            num[a[m][i]]++;
            maxx=max(num[a[m][i]],maxx);
            num[a[m][n-1-i]]++;
            maxx=max(num[a[m][n-1-i]],maxx);
            num[a[n-1-i][m]]++;
            maxx=max(num[a[n-1-i][m]],maxx);
            num[a[i][m]]++;
            maxx=max(num[a[i][m]],maxx);
            sum+=(4-maxx);
        }
        for(int i=0; i<n/2; i++)
        {
            maxx=1;
            map<char,int>num;
            num[a[i][i]]++;
            maxx=max(num[a[i][i]],maxx);
            num[a[n-1-i][n-1-i]]++;
            maxx=max(num[a[n-1-i][n-1-i]],maxx);
            num[a[n-1-i][i]]++;
            maxx=max(num[a[n-1-i][i]],maxx);
            num[a[i][n-1-i]]++;
            maxx=max(num[a[i][n-1-i]],maxx);
            sum+=(4-maxx);
        }
        printf("%d\n",sum);
    }
    return 0;
}


你可能感兴趣的:(zoj3838浙大月赛 大模拟)