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.
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.
而且更改的字符要是最少的
四轴对称是指关于 横着的中间的轴 竖着的中间的轴 主对角线 副对角线
一开始根据题目yy的是关于中心位置 中心对称的。。
根据一个点去搜索这个点跟哪些点有四轴对称的关系 把这些点都找到
记录这些点中出现的字符及个数 选最大的那个 其他的字符全都变成这个字符
记录个数
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <queue>
#define eps 1e-8
#define op operator
#define MOD 10009
#define MAXN 100100
#define INF 0x7fffffff
#define MEM(a,x) memset(a,x,sizeof a)
#define ll __int64
using namespace std;
char ch[110][110];
int num[300];
int vis[110][110];
int ans;
int n;
struct node
{
int x,y;
};
queue<node> Q;
void bfs(int x,int y)
{
vis[x][y]=1;
node p;
p.x=x; p.y=y;
Q.push(p);
MEM(num,0);
num[ch[p.x][p.y]]++;
while(!Q.empty())
{
node q=Q.front(); Q.pop();
int xx=q.x; int yy=q.y;
int xxx,yyy;
xxx=n-1-xx; yyy=n-1-yy;
if(!vis[xx][yyy])
{
vis[xx][yyy]=1;
p.x=xx; p.y=yyy;
Q.push(p);
num[ch[p.x][p.y]]++;
}
if(!vis[xxx][yy])
{
vis[xxx][yy]=1;
p.x=xxx; p.y=yy;
Q.push(p);
num[ch[p.x][p.y]]++;
}
xxx=yy; yyy=xx;
if(!vis[xxx][yyy])
{
vis[xxx][yyy]=1;
p.x=xxx; p.y=yyy;
Q.push(p);
num[ch[p.x][p.y]]++;
}
xxx=n-1-yy; yyy=n-1-xx;
if(!vis[xxx][yyy])
{
vis[xxx][yyy]=1;
p.x=xxx; p.y=yyy;
Q.push(p);
num[ch[p.x][p.y]]++;
}
}
int s=0;
int mx=-1;
for(int i=0;i<256;i++)
{
mx=max(mx,num[i]);
s+=num[i];
}
// cout<<"s:"<<s<<" mx:"<<mx<<endl;
ans+=(s-mx);
}
int main()
{
// freopen("ceshi.txt","r",stdin);
int tc;
scanf("%d",&tc);
while(tc--)
{
scanf("%d",&n);
getchar();
for(int i=0; i<n; i++)
scanf("%s",ch[i]);
ans=0;
MEM(vis,0);
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
if(!vis[i][j])
bfs(i,j);
printf("%d\n",ans);
}
return 0;
}