HDU 4772 Zhuge Liang's Password (矩阵旋转)

Zhuge Liang‘s Password

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 931    Accepted Submission(s): 641

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.

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

Output

  For each test case, print the password.

Sample Input

2
1 2
3 4
5 6
7 8
2
10 20
30 13
90 10
13 21
0

Sample Output

0
2

Source

2013 Asia Hangzhou Regional Contest

Recommend

题目意思就是把矩阵旋转,然后新矩阵和另一个矩阵有多少相同的,旋转90,180,270,360度,相同的数字的最大值就是密码。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<stdlib.h>
#include<algorithm>
using namespace std;
const int MAXN=30+5;
int a[MAXN][MAXN],b[MAXN][MAXN],n,maxn;
void change()
{
    int c[MAXN][MAXN];
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            c[i][j]=a[j][n-1-i]; //矩阵旋转,模拟一下就出来
    for(int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            a[i][j]=c[i][j];
}
int main()
{
    while(scanf("%d",&n)&&n)
    {
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                scanf("%d",&a[i][j]);
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
                scanf("%d",&b[i][j]);
        maxn=-1;
        for(int i=0;i<4;i++)
        {
            int cnt=0;
            for(int j=0;j<n;j++)
                for(int k=0;k<n;k++)
                    if(a[j][k]==b[j][k])
                        cnt++;
            maxn=max(cnt,maxn);
            change();
        }
        printf("%d\n",maxn);
    }
    return 0;
}

HDU 4772 Zhuge Liang's Password (矩阵旋转)

时间: 2024-10-10 16:47:45

HDU 4772 Zhuge Liang's Password (矩阵旋转)的相关文章

HDU 4772 Zhuge Liang&#39;s Password 选择矩阵

本题需要使用选择矩阵的程序求解,这个和Leetcode上的一个程序是一样道理的.如果使用额外空间,那么是很容易做到的,这里不使用额外空间,直接使用到位操作,空间效率是O(1),这是个非常漂亮的到位旋转程序. 题意还是很重要,这次看错了一句话,就WA了一次: The maximum amount of cells which contains two equal numbers after overlapping, is the password. 这里是需要每次旋转之后求相同数字的最大值,而不是

HDU 4772 Zhuge Liang&#39;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

HDU 4772 Zhuge Liang&#39;s Password (简单模拟题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4772 题面: Zhuge Liang's Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1404    Accepted Submission(s): 926 Problem Description In the anc

HDU 4772 Zhuge Liang&#39;s Password(模拟水)

HDU 4772 Zhuge Liang's Password 题目链接 题意:给定两张牌,可以旋转后重叠,重合后相同数字最多的是密码,求密码 思路:直接模拟记录最大值即可 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int N = 35; int n; int a[N][N], b[N][N]; int solve() { int

HDU 4048 Zhuge Liang&#39;s Stone Sentinel Maze

Zhuge Liang's Stone Sentinel Maze Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 385    Accepted Submission(s): 106 Problem Description Zhuge Liang was a chancellor of the state of Shu Han dur

hdu 4739 Zhuge Liang&#39;s Mines 随机化

Zhuge Liang's Mines Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4739 Description In the ancient three kingdom period, Zhuge Liang was the most famous and smartest military leader. His enemy was Shima Yi, who

2018 Multi-University Training Contest 4 Problem J. Let Sudoku Rotate 【DFS+剪枝+矩阵旋转】

任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6341 Problem J. Let Sudoku Rotate Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1363    Accepted Submission(s): 717 Problem Description Sudoku i

HDU 4965 Fast Matrix Calculation 【矩阵】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4965 题目大意:给你一个N*K的矩阵A以及一个K*N的矩阵B (4 <= N <= 1000)以及 (2 <=K <= 6),然后接下来四步: 算一个新的矩阵C=A*B 算M=C^ (N*N) 对于M中的每个元素%6 将M中每个元素加起来,算出和. 也就是求出A*B * A*B * A*B * A*B * A*B *--* A*B   但是A*B形成的矩阵是N*N,而N大小有可能是10

HDU 5171 GTY&#39;s birthday gift 矩阵快速幂

点击打开链接 GTY's birthday gift Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 225    Accepted Submission(s): 78 Problem Description FFZ's birthday is coming. GTY wants to give a gift to ZZF. He as