Palindromic Matrix~解题报告

Palindromic Matrix

题目描述:

Let’s call some square matrix with integer values in its cells palindromic if it doesn’t change after the order of rows is reversed and it doesn’t change after the order of columns is reversed.

For example, the following matrices are palindromic:

The following matrices are not palindromic because they change after the order of rows is reversed:

The following matrices are not palindromic because they change after the order of columns is reversed:

You are given n2n2 integers. Put them into a matrix of nn rows and nn columns so that each number is used exactly once, each cell contains exactly one number and the resulting matrix is palindromic. If there are multiple answers, print any. If there is no solution, print “NO”.

Input:

The first line contains one integer nn (1≤n≤201≤n≤20).

The second line contains n2n2 integers a1,a2,…,an2a1,a2,…,an2 (1≤ai≤10001≤ai≤1000) — the numbers to put into a matrix of nn rows and nn columns.

Output:

If it is possible to put all of the n2n2 numbers into a matrix of nn rows and nn columns so that each number is used exactly once, each cell contains exactly one number and the resulting matrix is palindromic, then print “YES”. Then print nn lines with nn space-separated numbers — the resulting matrix.

If it’s impossible to construct any matrix, then print “NO”.

You can print each letter in any case (upper or lower). For example, “YeS”, “no” and “yES” are all acceptable.

Sample Input:

4
1 8 8 1 2 2 2 2 2 2 2 2 1 8 8 1

Sample Output:

YES
1 2 2 1
8 2 2 8
8 2 2 8
1 2 2 1

Sample Input:

3
1 1 1 1 1 3 3 3 3

Sample Output:

YES
1 3 1
3 1 3
1 3 1

Sample Input:

4
1 2 1 9 8 4 3 8 8 3 4 8 9 2 1 1

Sample Output:

NO

Sample Input:

1
10

Sample Output:

YES
10 

题目描述:

第一行输入N,第二行输入NN个数字,要求在NN方格中,组成一个在行与列交换顺序后依然是相同的回文方格,如果组成不了就输出NO
可以组成就输出YES,以及组成后的方格数字。

思路分析:

这道题我一开始直接简单的想成了奇偶末与尾的匹配,结果发现自己思路是多么愚蠢,看了一些大佬的代码之后就慢慢懂了,首先当输入N为偶数的时候,每一个数字必须要满足有4个的条件下进行,在奇数需要做不同的分配,要考虑行列与轴之间的对称,并且在中心中还需要稍作处理。

代码:

#include<cstdio>
#include<algorithm>
#include<map>
#include<iostream>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
map<int,int> visit;//用二维数组代替也行 
int temp;
int a[40][40];
int LemonGet(int ans)//这一步是为了判断数字是否达到要求数目 
{
	map<int,int> ::iterator it = visit.begin();//创建it指针指向map的头 
	for(it = visit.begin();it != visit.end();++it)
	{
		if(it->second >= ans)//这里讲解一下second与first分别代表的是i以及visit[i]的数值 
		{
			it->second-=ans;//大于后就减去防止后面处理干扰 
			return it->first;
		}
	}
	return -1;
}
void LemonSolve()
{
	int num; 
		for(int i=1;i<=temp/2;i++)//这里奇和偶数都能实用,大佬果然不同 
		{
		for(int j=1;j<=temp/2;j++)
		{
			num=LemonGet(4);
			if(num==-1)//达不到要求说明无法组成回文格 
			{
				printf("NO\n");
				return;
			}
			else
			{
				a[i][j] = a[temp+1-i][j] = a[i][temp+1-j] = a[temp+1-i][temp+1-j] = num;
				//这里四个方向分别是上下左右,直接拿图写写就知道了,这里就不做提示
				//都是对称就行	
			}
		}
	}
	if(temp%2!=0)//奇数的时候需要在做处理 
	{
	for(int i=1;i<=temp/2;i++)//这里需要每一步每一步判断了 
		{
			num = LemonGet(2);//处理中间列的对称(1 x 1
							 //                   1 1 1
							 //                   1 x 1处理x值,这里代入是2,是因为
							 //先前的外部以被上述所填补,这里就不做解释了,中间1是为了好看 
			if(num==-1)
			{
				printf("NO\n");
				return;
			 } 
			 else
			 {
			 	a[i][(temp+1)/2]=a[(temp+1)-i][(temp+1)/2]=num;
			 }
			 num = LemonGet(2);//处理中间列的对称(1 1 1
							 //                    x 1 x
							 //                    1 1 1处理x值,
			if(num==-1)
			{
				printf("NO\n");
				return;
			 } 
			 else
			 {
			 	a[(temp+1)/2][temp+1-i]=a[(temp+1)/2][i]=num;
			 }
		}
		num = LemonGet(1);//注意这里是放在循环外面的,特别处理中心部分
							//处理中间列的对称(1 1 1
							 //                 1 x 1
							 //                 1 1 1处理x值, 
			if(num==-1)
			{
				printf("NO\n");
				return;
			 } 
			 else
			 {
			 	a[(temp+1)/2][(temp+1)/2]=num;
			 }
	}
	printf("YES\n");//输出YES与九宫格后的样子 
	for(int i=1;i<=temp;i++)
	{
		for(int j=1;j<=temp;j++)
		{
			printf("%d%c",a[i][j],j == temp ? '\n' : ' ');//达到格式 
		}
}
}
int main()
{
	int b;
	while(scanf("%d",&temp)!=EOF)//得到N 
	{
		visit.clear();//map红黑子树节点清除 
		for(int i=1;i<=temp*temp;i++)//得到N*N个数字 
		{
			scanf("%d",&b);
			visit[b]++;	//记录每个输入数字的数目 
		}
		LemonSolve();//进入输出 
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章