Kongming’s Password
Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 512 megabytes
In the ancient period of Three Kingdoms, Zhuge Kongming, the Prime Minister of Kingdom Shu, was the most famous and wise statesman. His enemy was Sima Zhongda, the Grand Preceptor of Kingdom Wei. Zhongda always looked stupid when fighting against Kongming. But it was Zhongda who laughed to the end. Kongming had led his army across the Mountain Qi to attack Kingdom Wei six times, which all failed. Because of the long journey, the food supply was a big problem.Kongming invented a kind of transportation robot called “Wooden Bull & Floating Horse” (in abbreviation,WBFH) to carry food for the army. Every WBFH had a password lock. A WBFH would move if and only if a soldier entered the correct password. Kongming was always worrying about everything and always did trivial things by himself. Since he put Ma Youchang to death due to losing the Battle of Jieting, Kongming did not trust anyone’s IQ anymore. He thought the soldiers might forget the password of WBFHs. So he made two password cards for each WBFH. If the soldier operating a WBFH forgot the password or got killed, the password still could be recovered by those two password cards.Once, Zhongda defeated Kongming again and got many WBFHs on the battlefield. But he did not know the passwords. Youchang’s son betrayed Kongming and came to Zhongda. He told Zhongda the way to figure out the password from the two cards. He said to Zhongda: “A password card is a square grid consisting of N × N cells. In each cell, there is a number. Two password cards are of the same size. If you overlap them, you get two numbers in each cell. Those two numbers in a cell may be the same or different. You can turn a card by 0 degree, 90 degrees, 180 degrees, or 270 degrees, and then overlap it on another. But flipping is not allowed. The maximum amount of cells that contain two equal numbers after overlapping, is the password. Please note that the two cards must be totally overlapped. You cannot only overlap a part of them.” Now you should find a way to figure out the password for each WBFH as quickly as possible.
Input
There are several test cases. In each test case:
The first line contains an integer N (0 < N ≤ 30) — the password card is an N × N grid.
Then an N × N matrix follows, describing one password card. Each element is an integer in a cell.
Then another N × N matrix follows, describing the other password card.
Those integers are all no less than 0 and less than 300.
The input ends with N = 0.
2
1 2
3 4
5 6
7 8
2
10 20
30 13
90 10
13 21
0
Output
For each test case, print the password.
0
2
点击查看代码
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
while(cin>>n,n) {
int a[n+5][n+5],b[n+5][n+5];
int c[n+5][n+5];
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
cin>>a[i][j];
}
}
int ret = 0;
int cnt = 0;
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
cin>>b[i][j];
if(a[i][j]==b[i][j]) cnt++;
}
}
ret = max(cnt,ret);
for(int k = 0;k < 3;k++) {
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
c[i][j] = a[j][n-i-1];
}
}
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
a[i][j] = c[i][j];
}
}
/*
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
cout<<a[i][j]<<' ';
}
cout<<endl;
}
*/
cnt = 0;
for(int i = 0;i < n;i++) {
for(int j = 0;j < n;j++) {
if(a[i][j]==b[i][j]) cnt++;
}
}
ret = max(cnt,ret);
}
}
return 0;
}
/*
逆时针旋转
1 2
3 4
3 1
4 2
4 3
2 1
2 4
1 3
-----
1 2
3 4
*/