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();//進入輸出 
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章