模拟 ZJU 3838

上,下,左,右,斜都要对称,求最少的步骤

Infusion Altar

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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.

InfusionAltar

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值