HDU 4772 Zhuge Liang's Password 选择矩阵

本题需要使用选择矩阵的程序求解,这个和Leetcode上的一个程序是一样道理的。如果使用额外空间,那么是很容易做到的,这里不使用额外空间,直接使用到位操作,空间效率是O(1),这是个非常漂亮的到位旋转程序。

题意还是很重要,这次看错了一句话,就WA了一次:

The maximum amount of cells which contains two equal numbers after overlapping, is the password.

这里是需要每次旋转之后求相同数字的最大值,而不是所有的相同数字之和。太快了审题错误啊。细心细心,再细心。

#include <stdio.h>
#include <vector>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <limits.h>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;

const int MAX_N = 31;
int card1[MAX_N][MAX_N], card2[MAX_N][MAX_N], N;

void rotate90()
{
	for (int i = 0, j = N-1; i < j; i++, j--)
	{
		for (int k = i, d = j; k < j; k++, d--)
		{
			int t = card1[i][k];
			card1[i][k] = card1[d][i];
			card1[d][i] = card1[j][d];
			card1[j][d] = card1[k][j];
			card1[k][j] = t;
		}
	}
}

int main()
{
	while (~scanf("%d", &N) && N)
	{
		for (int i = 0; i < N; i++)
		{
			for (int j = 0; j < N; j++)
			{
				scanf("%d", &card1[i][j]);
			}
		}
		int pw = 0;
		for (int i = 0; i < N; i++)
		{
			for (int j = 0; j < N; j++)
			{
				scanf("%d", &card2[i][j]);
				if (card1[i][j] == card2[i][j]) pw++;
			}
		}
		for (int k = 0; k < 3; k++)
		{
			rotate90();
			int tmp = 0;
			for (int i = 0; i < N; i++)
			{
				for (int j = 0; j < N; j++)
				{
					if (card1[i][j] == card2[i][j]) tmp++;
				}
			}
			if (tmp > pw) pw = tmp;
		}
		printf("%d\n", pw);
	}
	return 0;
}

HDU 4772 Zhuge Liang's Password 选择矩阵,布布扣,bubuko.com

HDU 4772 Zhuge Liang's Password 选择矩阵

时间: 2024-09-30 06:37:15

HDU 4772 Zhuge Liang's Password 选择矩阵的相关文章

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

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

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 1757 A Simple Math Problem (乘法矩阵)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2441    Accepted Submission(s): 1415 Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) =

HDU 2842 Chinese Rings (带常数矩阵+矩阵快速幂)

HDU 2842 Chinese Rings (带常数矩阵+矩阵快速幂) ACM 题目地址:HDU 2842 Chinese Rings 题意: 一种中国环,解开第k个环需要先解开前(k-2)个环,并留有第(k-1)环.问解开n环最少需要几步. 分析: 设f(n)表示解开n环. 1. 由于游戏规则,解开n环不能一下子把n-1全解开了,否则第n个就没法拿掉了. 2. 得先拿掉第n个:先完成f(n-2),然后再拿掉第n环. 3. 然后放回前(n-2),其实这也是f(n-2),因为是一个逆的过程. 4

HDU 4686 Arc of Dream(快速幂矩阵)

题目链接 再水一发,构造啊,初始化啊...wa很多次啊.. #include <cstring> #include <cstdio> #include <string> #include <iostream> #include <algorithm> #include <vector> #include <queue> using namespace std; #define MOD 1000000007 #define