上,下,左,右,斜都要对称,求最少的步骤
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.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define MAXN 100 + 10
char map[MAXN][MAXN];
struct node{
int x,y;
}a[1000];
int Max(int a,int b){
return a>b?a:b;
}
int main(){
int T,n,i,j,k,jj;
int ans;
while(~scanf("%d",&T)){
while(T--){
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%s",map[i]);
}
if(map[n/2][n/2] == '#'){
ans = 0;
}
else{
ans = 1;
}
for(k=0;k<n/2;k++){//代表第几行
for(i=k;i<=n/2;i++){//代表该行第几列
if(i == k){
int mark=0;
int max1=-1;
a[0].x=i,a[0].y=i;
a[1].x=k,a[1].y=n-1-i;
a[2].x=n-1-i,a[2].y=k;
a[3].x=n-1-i,a[3].y=n-1-i;
for(j=0;j<4;j++){
mark = 1;
for(jj=0;jj<4;jj++){
if(j!=jj){
if(map[a[j].x][a[j].y] == map[a[jj].x][a[jj].y]){
mark++;
}
}
}
max1 = Max(max1,mark);
}
ans+=(4-max1);
}
else if(i == n/2){
a[0].x=k,a[0].y=n/2;
a[1].x=n/2,a[1].y=n-1-k;
a[2].x=n-1-k,a[2].y=n/2;
a[3].x=n/2,a[3].y=k;
int mark;
int max1 = -1;
for(j=0;j<4;j++){
mark = 1;
for(jj=0;jj<4;jj++){
if(jj!=j){
if(map[a[j].x][a[j].y] == map[a[jj].x][a[jj].y]){
mark++;
}
}
}
max1 = Max(mark,max1);
}
ans+=(4-max1);
}
else{
a[0].x=k,a[0].y=i;
a[1].x=i,a[1].y=k;
a[2].x=k,a[2].y=n-1-i;
a[3].x=i,a[3].y=n-k-1;
a[4].x=n-1-i,a[4].y=n-k-1;
a[5].x=n-k-1,a[5].y=n-1-i;
a[6].x=n-k-1,a[6].y=i;
a[7].x=n-1-i,a[7].y=k;
int mark;
int max1 = -1;
for(j=0;j<8;j++){
mark = 1;
for(jj=0;jj<8;jj++){
if(jj!=j){
if(map[a[j].x][a[j].y] == map[a[jj].x][a[jj].y]){
mark++;
}
}
}
max1 = Max(mark,max1);
}
ans+=(8-max1);
}
}
}
printf("%d\n",ans);
}
}
return 0;
}