首页 > 代码库 > ZOJ 3838 Infusion Altar(数学啊 模拟啊 这么挫的代码你见过吗?)
ZOJ 3838 Infusion Altar(数学啊 模拟啊 这么挫的代码你见过吗?)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5412
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.Author: ZHU, Jiale; GONG, Yuan
Source: ZOJ Monthly, November 2014
题意:
求最少的操作次数使图形对称!
PS:
1、先分别找出要使四条对称轴对称的最少操作次数,(显然只需要分别找出其中一条红色线和其中一条白色线的一半使八个部分一样即可);
2、再枚举1、2、3……号格子分别和其余七个格子相同最少所需的次数!
代码如下:(这么挫的代码你见过吗?)
#include <cstdio> #include <cstring> #include <cmath> int main() { int t; int n; char s[117][117]; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i = 0; i < n; i++) { scanf("%s",s[i]); } int m = n/2; int sum = 0; int minn; char tt; for(int i = 0; i < m; i++)//中线 { minn = 4; tt = s[i][m];//1 int k1 = 0; if(tt != s[m][i]) k1++; if(tt != s[n-1-i][m]) k1++; if(tt != s[m][n-1-i]) k1++; if(minn > k1) minn = k1; tt = s[m][i];//2 int k2 = 0; if(tt != s[i][m]) k2++; if(tt != s[n-1-i][m]) k2++; if(tt != s[m][n-1-i]) k2++; if(minn > k2) minn = k2; tt = s[n-1-i][m];//3 int k3 = 0; if(tt != s[m][i]) k3++; if(tt != s[i][m]) k3++; if(tt != s[m][n-1-i]) k3++; if(minn > k3) minn = k3; tt = s[m][n-1-i];//4 int k4 = 0; if(tt != s[m][i]) k4++; if(tt != s[n-1-i][m]) k4++; if(tt != s[i][m]) k4++; if(minn > k4) minn = k4; sum+=minn; } for(int i = 0; i < m; i++)//主副对角线 { minn = 4; tt = s[i][i];//1 int k1 = 0; if(tt != s[n-1-i][n-1-i]) k1++; if(tt != s[n-1-i][i]) k1++; if(tt != s[i][n-1-i]) k1++; if(minn > k1) minn = k1; tt = s[n-1-i][n-1-i];//2 int k2 = 0; if(tt != s[i][i]) k2++; if(tt != s[n-1-i][i]) k2++; if(tt != s[i][n-1-i]) k2++; if(minn > k2) minn = k2; tt = s[n-1-i][i];//3 int k3 = 0; if(tt != s[n-1-i][n-1-i]) k3++; if(tt != s[i][i]) k3++; if(tt != s[i][n-1-i]) k3++; if(minn > k3) minn = k3; tt = s[i][n-1-i];//4 int k4 = 0; if(tt != s[n-1-i][n-1-i]) k4++; if(tt != s[n-1-i][i]) k4++; if(tt != s[i][i]) k4++; if(minn > k4) minn = k4; sum+=minn; } //printf("sum::%d\n",sum); //int cen = ((n/2)*(n/2)-(n/2))/2; int cen = n/2; //printf("cen::%d\n",cen); for(int i = 0; i < cen-1; i++) { int cc = 0; for(int j = i+1; j < cen; j++) { minn = 8; tt = s[i][j];//1 //printf("s[i][j]:%c\n",s[i][j]); cc = 0; if(tt != s[j][i]) cc++; //printf("s[i][j]:%c\n",s[j][i]); if(tt != s[i][n-1-j]) cc++; //printf("s[i][n-1-j]:%c\n",s[i][n-1-j]); if(tt != s[n-1-j][i]) cc++; //printf("s[n-1-j][i]:%c\n",s[n-1-j][i]); if(tt != s[j][n-1-i]) cc++; //printf("s[j][n-1-i]:%c\n",s[j][n-1-i]); if(tt != s[n-1-i][j]) cc++; //printf("s[n-1-i][j]:%c\n",s[n-1-i][j]); if(tt != s[n-1-j][n-1-i]) cc++; //printf("s[n-1-j][n-1-i]:%c\n",s[n-1-j][n-1-i]); if(tt != s[n-1-i][n-1-j]) cc++; //printf("s[n-1-i][n-1-j]:%c\n",s[n-1-i][n-1-j]); if(cc < minn) minn = cc; tt = s[j][i];//2 cc = 0; if(tt != s[i][j]) cc++; if(tt != s[i][n-1-j]) cc++; if(tt != s[n-1-j][i]) cc++; if(tt != s[j][n-1-i]) cc++; if(tt != s[n-1-i][j]) cc++; if(tt != s[n-1-j][n-1-i]) cc++; if(tt != s[n-1-i][n-1-j]) cc++; if(cc < minn) minn = cc; tt = s[i][n-1-j];//3 cc = 0; if(tt != s[j][i]) cc++; if(tt != s[i][j]) cc++; if(tt != s[n-1-j][i]) cc++; if(tt != s[j][n-1-i]) cc++; if(tt != s[n-1-i][j]) cc++; if(tt != s[n-1-j][n-1-i]) cc++; if(tt != s[n-1-i][n-1-j]) cc++; if(cc < minn) minn = cc; tt = s[n-1-j][i];//4 cc = 0; if(tt != s[j][i]) cc++; if(tt != s[i][n-1-j]) cc++; if(tt != s[i][j]) cc++; if(tt != s[j][n-1-i]) cc++; if(tt != s[n-1-i][j]) cc++; if(tt != s[n-1-j][n-1-i]) cc++; if(tt != s[n-1-i][n-1-j]) cc++; if(cc < minn) minn = cc; tt = s[j][n-1-i];//5 cc = 0; if(tt != s[j][i]) cc++; if(tt != s[i][n-1-j]) cc++; if(tt != s[n-1-j][i]) cc++; if(tt != s[i][j]) cc++; if(tt != s[n-1-i][j]) cc++; if(tt != s[n-1-j][n-1-i]) cc++; if(tt != s[n-1-i][n-1-j]) cc++; if(cc < minn) minn = cc; tt = s[n-1-i][j];//6 cc = 0; if(tt != s[j][i]) cc++; if(tt != s[i][n-1-j]) cc++; if(tt != s[n-1-j][i]) cc++; if(tt != s[j][n-1-i]) cc++; if(tt != s[i][j]) cc++; if(tt != s[n-1-j][n-1-i]) cc++; if(tt != s[n-1-i][n-1-j]) cc++; if(cc < minn) minn = cc; tt = s[n-1-j][n-1-i];//7 cc = 0; if(tt != s[j][i]) cc++; if(tt != s[i][n-1-j]) cc++; if(tt != s[n-1-j][i]) cc++; if(tt != s[j][n-1-i]) cc++; if(tt != s[n-1-i][j]) cc++; if(tt != s[i][j]) cc++; if(tt != s[n-1-i][n-1-j]) cc++; if(cc < minn) minn = cc; tt = s[n-1-i][n-1-j];//8 cc = 0; if(tt != s[j][i]) cc++; if(tt != s[i][n-1-j]) cc++; if(tt != s[n-1-j][i]) cc++; if(tt != s[j][n-1-i]) cc++; if(tt != s[n-1-i][j]) cc++; if(tt != s[n-1-j][n-1-i]) cc++; if(tt != s[i][j]) cc++; if(cc < minn) minn = cc; sum+=minn; } } printf("%d\n",sum); } return 0; } /* 99 3 o.o .#. o.o 5 .aab. bo.ob b.#.a bo.ob bbab. 5 aabba ao.oa a.#.a ao.oa aaaaa 5 5 .1a2. 3o.o4 b.#.a 5o.o6 b7a8. */
ZOJ 3838 Infusion Altar(数学啊 模拟啊 这么挫的代码你见过吗?)