ZOJ-3838-Infusion Altar

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
.#.
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.

题目链接:ZOJ 3838

题目大意:根据这四条线对称。圆圈对称8个相等,线上对称4个相等。
o.oZOJ-3838-Infusion Altar_第1张图片

以下是代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#include <vector>

using namespace std;
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        string s[110];
        int ans = 0,cnt = 0;
        for (int i = 0; i < n; i++)
            cin >> s[i];
        for (int i = 0; i < n / 2; i++)
        {
            for (int j = 0; j < n / 2; j++)
            {
                if (i > j)              //处理圆圈处,对称8个相等
                {
                    cnt = 1;
                    int maxcnt = 1;
                    string ret = "";
                    ret += s[i][j];
                    ret += s[j][i];
                    ret += s[n - 1 - i][j];
                    ret += s[j][n - i - 1];
                    ret += s[i][n - 1 - j];
                    ret += s[n - 1 - j][i];
                    ret += s[n - 1 - i][n - 1 - j];
                    ret += s[n - 1 - j][n - 1 - i];         
                    sort(ret.begin(),ret.end());
                    for (int i = 0;i < 7; i++)
                    {
                        if (ret[i] == ret[i + 1])
                        {
                            cnt++;
                            maxcnt = max(cnt,maxcnt);
                        }
                        else
                            cnt = 1;
                    }
                    ans += 8 - maxcnt;                      
                }
                else if (i == j)      //处理对角线
                {
                    cnt = 1;
                    int maxcnt = 1;
                    string ret = "";
                    ret += s[i][j];
                    ret += s[i][n - j - 1];
                    ret += s[n - i - 1][j];
                    ret += s[n - i - 1][n - j - 1];
                    sort(ret.begin(),ret.end());
                    for (int i = 0;i < 3; i++)
                    {
                        if (ret[i] == ret[i + 1])
                        {
                            cnt++;
                            maxcnt = max(cnt,maxcnt);
                        }
                        else
                            cnt = 1;
                    }
                    ans += 4 - maxcnt;                          
                }
            }
        }
        int i = (n - 1) / 2;         //处理另外两条直线
        for (int j = 0; j < n / 2; j++)
        {
            cnt = 1;
            int maxcnt = 1;
            string ret = "";
            ret += s[i][j];
            ret += s[i][n - j - 1];
            ret += s[j][i];
            ret += s[n - j - 1][i];
            sort(ret.begin(),ret.end());
            for (int i = 0;i < 3; i++)
                if (ret[i] == ret[i + 1])
                {
                    cnt++;
                    maxcnt = max(maxcnt,cnt);
                }
                else
                    cnt = 1;
            ans += 4 - maxcnt;
        }
        cout << ans << endl;
    }
    return 0;
}

你可能感兴趣的:(模拟)