Codeforces 1118C-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 n2 integers. Put them into a matrix of n rows and n 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 n (1≤n≤20).

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

Output
If it is possible to put all of the n2 numbers into a matrix of n rows and n 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 n lines with n 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.

Examples
inputCopy
4
1 8 8 1 2 2 2 2 2 2 2 2 1 8 8 1
outputCopy
YES
1 2 2 1
8 2 2 8
8 2 2 8
1 2 2 1
inputCopy
3
1 1 1 1 1 3 3 3 3
outputCopy
YES
1 3 1
3 1 3
1 3 1
inputCopy
4
1 2 1 9 8 4 3 8 8 3 4 8 9 2 1 1
outputCopy
NO
inputCopy
1
10
outputCopy
YES
10
Note

题解:

仔细观察可以看出来,如果n是偶数,那么矩阵没有轴,所以每种数字都必须4个4个出现,如果某个数的个数%4不是0则NO,模拟输出的时候就模拟1/4块就可以了
如果n是奇数,矩阵有轴,那么先模拟非轴的区域,找出4个4个出现的数字去填充;然后模拟轴上的区域,找出若干个剩余数量还在2个以上的数来填充轴就可以了,如果过程中找不出数了就NO

代码:

#include<stdio.h>
int ans[1005][1005];
int main()
{
	int n,a[1005],sum[1005],b,k;
	scanf("%d",&n);
	for(int i=0;i<1005;i++)sum[i]=0;
	for(int i=0;i<n*n;i++)
	{
		scanf("%d",&b);
		int flag=0;
		for(int j=0;j<k;j++)
		{
			if(b==a[j]&&sum[j]<4)
			{
				sum[j]++;
				flag=1;
				break;
			}
		}
		if(flag==0)
		{
			a[k]=b;
			sum[k]=1;
			k++;
		}
	}
	if(n%2==0)
	{
		for(int i=0;i<k;i++)
		{
			if(sum[i]!=4)
			{
				printf("NO\n");
				return 0;
			}
		}
		printf("YES\n");
		int s=0;
		for(int i=0;i<n/2;i++)
		{
			for(int j=0;j<n/2;j++)
			{
				ans[i][j]=a[s];
				ans[i][n-1-j]=a[s];
				ans[n-1-i][j]=a[s];
				ans[n-1-i][n-1-j]=a[s];
				s++;
			}
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(j==n-1)printf("%d\n",ans[i][j]);
				else printf("%d ",ans[i][j]);
			}
		}
	}
	else
	{
		int s=0;
		for(int i=0;i<n/2;i++)
		{
			for(int j=0;j<n/2;j++)
			{
				while(sum[s]!=4)
				{
					s++;
					if(s>=k)
					{
						printf("NO\n");
						return 0;
					}
				}
				ans[i][j]=a[s];
				ans[i][n-1-j]=a[s];
				ans[n-1-i][j]=a[s];
				ans[n-1-i][n-1-j]=a[s];
				sum[s]=0;
			}
		}
		s=0;
		for(int i=0;i<n/2;i++)
		{
			while(sum[s]<2)
			{
				s++;
				if(s>=k)
				{
					printf("NO\n");
					return 0;
				}
			}
			ans[n/2][i]=a[s];
			ans[n/2][n-1-i]=a[s];
			sum[s]-=2;
		}
		s=0;
		for(int i=0;i<n/2;i++)
		{
			while(sum[s]<2)
			{
				s++;
				if(s>=k)
				{
					printf("NO\n");
					return 0;
				}
			}
			ans[i][n/2]=a[s];
			ans[n-1-i][n/2]=a[s];
			sum[s]-=2;
		}
		for(int i=0;i<k;i++)if(sum[i])ans[n/2][n/2]=a[i];
		printf("YES\n");
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(j==n-1)printf("%d\n",ans[i][j]);
				else printf("%d ",ans[i][j]);
			}
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值