首页 > 代码库 > HDU 4772 Zhuge Liang's Password
HDU 4772 Zhuge Liang's Password
Problem Description
In the ancient three kingdom period, Zhuge Liang was the most famous and smart military leader. His enemy was Sima Yi, the military leader of Kingdom Wei. Sima Yi always looked stupid when fighting against Zhuge Liang. But it was Sima Yi who laughed to the end.
Zhuge Liang had led his army across the mountain Qi to attack Kingdom Wei for six times, which all failed. Because of the long journey, the food supply was a big problem. Zhuge Liang invented a kind of bull-like or horse-like 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 the soldier entered the password. Zhuge Liang was always worrying about everything and always did trivial things by himself. Since Ma Su lost Jieting and was killed by him, he didn‘t trust anyone‘s IQ any more. 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 regained by those two password cards.
Once, Sima Yi defeated Zhuge Liang again, and got many WBFHs in the battle field. But he didn‘t know the passwords. Ma Su‘s son betrayed Zhuge Liang and came to Sima Yi. He told Sima Yi the way to figure out the password by two cards.He said to Sima Yi:
"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 not the same. 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 which contains two equal numbers after overlapping, is the password. Please note that the two cards must be totally overlapped. You can‘t only overlap a part of them."
Now you should find a way to figure out the password for each WBFH as quickly as possible.
Zhuge Liang had led his army across the mountain Qi to attack Kingdom Wei for six times, which all failed. Because of the long journey, the food supply was a big problem. Zhuge Liang invented a kind of bull-like or horse-like 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 the soldier entered the password. Zhuge Liang was always worrying about everything and always did trivial things by himself. Since Ma Su lost Jieting and was killed by him, he didn‘t trust anyone‘s IQ any more. 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 regained by those two password cards.
Once, Sima Yi defeated Zhuge Liang again, and got many WBFHs in the battle field. But he didn‘t know the passwords. Ma Su‘s son betrayed Zhuge Liang and came to Sima Yi. He told Sima Yi the way to figure out the password by two cards.He said to Sima Yi:
"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 not the same. 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 which contains two equal numbers after overlapping, is the password. Please note that the two cards must be totally overlapped. You can‘t 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 a integer N, meaning that the password card is a N×N grid(0<N<=30).
Then a N×N matrix follows ,describing a password card. Each element is an integer in a cell.
Then another N×N matrix follows, describing another password card.
Those integers are all no less than 0 and less than 300.
The input ends with N = 0
In each test case:
The first line contains a integer N, meaning that the password card is a N×N grid(0<N<=30).
Then a N×N matrix follows ,describing a password card. Each element is an integer in a cell.
Then another N×N matrix follows, describing another password card.
Those integers are all no less than 0 and less than 300.
The input ends with N = 0
Output
For each test case, print the password.
Sample Input
2 1 2 3 4 5 6 7 8 10 20 30 13 90 10
Sample Output
02
题意:给你2个n*n的正方形,要你求出这2个正方形沿着正方形对折后,任意一个正方形 可以旋转90 180 270 0度,要你求出旋转后最大的重合数的个数!
思路:这是一道典型的模拟题,也可以说这是一道搜索的题目,所以我们需要枚举这几种情况出来,每次枚举之后,求出重合的数的个数就可以了!每次旋转的时候我们可以明显地发现,一行一行地转换时,以旋转90度为一次
AC代码:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int a1[35][35]; int a2[35][35]; int a3[35][35]; int n; void flap() //旋转函数 { int i,j; for(i=0;i<n;i++) //将a2旋转后记录在a2里 { for(j=0;j<n;j++) a3[j][n-i-1]=a2[i][j]; } for(i=0;i<n;i++) { for(j=0;j<n;j++) a2[i][j]=a3[i][j]; } } int dfs(int flag) { int i,j; int tot=0; for(i=0;i<n;i++)//记录每次重合的数的个数 { for(j=0;j<n;j++) if(a1[i][j]==a2[i][j]) tot++; } return tot; } int main() { int i,j; while(scanf("%d",&n)!=EOF) { if(n==0)break; for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&a1[i][j]); for(i=0;i<n;i++) for(j=0;j<n;j++) scanf("%d",&a2[i][j]); int sum=dfs(1);//旋转90度 for(i=2;i<=4;i++) //每次只要在原有的基础上再旋转90就可以了 { flap(); sum=max(sum,dfs(i)); } printf("%d\n",sum); } return 0; }
HDU 4772 Zhuge Liang's Password
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。