cf 1118C Palindromic Matrix

Topic Link

I. Introduction

Just think of ideas: happy, so simple

Ready to start realization: emmmmm, a bit complicated

Read, read: This is cf title? ?

Paid the: what is this wrong? ? Local and assessment are not the same? ? ? ?

Help me figure out the wrong hat later, after: Oh, I am so stay right. Oh, cf re not report it? Well, certainly not my pot, it is not an error, it is so

Second, 题意

It gives the number of n * n, palindromic matrix configuration. I.e., the number of palindromic matrix each row or each column, in reverse order, the matrix does not change.

Third, the idea

1. This palindrome requirements, think about it, if a palindrome matrix is ​​a matrix, then just know that it is a box at the top left of the number, we can draw a symmetrical matrix of the whole number.

2. If n is even, the submatrices may be symmetrical by the n / 2 * n / 2 of the large matrix; if n is odd, it is imperative intermediate half line, half the number of columns is also determined, i.e., determining (n + submatrix 1) / 2 * (n + 1) / 2, thereby to obtain a large matrix.

3. The method of determining the number: the first number, such as found in the upper left corner, to find the number of the counter of the number 1 to number 4 is greater than the first 1000 and set it to the upper left corner of the first number, and Save 4.

4. After determining the number of complete, according to the global rule it symmetrically.

5. Happy simulate the gods

Fourth, the code

#include<bits/stdc++.h>
using namespace std;
int a[40][40],c[1005];
void End()
{
    cout<<"NO"<<endl;
}
int main()
{
    memset(a,0,sizeof(a));
    memset(c,0,sizeof(c));
    int n;
    cin>>n;
    for(int i=0;i<n*n;i++)
    {
        int x;
        cin>>x;
        c[x]++;
    }
    //偶数
    if(n%2==0)
    {
        vector<int> v;
        for(int i=1;i<=1000;i++)//遍历1到1000
        {
            if(c[i]>0&&c[i]%4!=0) {End();return 0;}//有个数不是4的倍数的数
            if(c[i]>0)
            {
                c[i]-=4;
                v.push_back(i);
                i--;
            }
        }
        //用找到的这些数把四分之一部分填满
        for(int i=0;i<v.size();i++)
        {
            int r,c,r1,c1;
            r=ceil((double)(i+1)/(n/2));
            c=(i+1)%(n/2); c= (c==0 ? n/2 : c) ;
            r1=n-r+1;
            c1=n-c+1;
            a[r][c]=a[r][c1]=a[r1][c]=a[r1][c1]=v[i];
        }
        cout<<"YES"<<endl;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
                cout<<a[i][j]<<' ';
            cout<<endl;
        }
    }
    //奇数
    else
    {
        //把左上方的数确定下来
        for(int i=1;i<=n/2*(n/2);i++)
        {
            int j=1;
            for(;j<=1000;j++)//把j挑出来
            {
                if(c[j]>=4)
                {
                    c[j]-=4;
                    int r,c,r1,c1;//第i个数的行列
                    r=ceil((double)i/(n/2));
                    c=i%(n/2); c= (c==0 ? n/2 : c) ;
                    r1=n-r+1;
                    c1=n-c+1;
                    a[r][c]=a[r][c1]=a[r1][c]=a[r1][c1]=j;
                    break;
                }
            }
            if(j==1001) {End();return 0;}
        }

        //把中间两条的数确定下来
        for(int i=1;i<=(n-1)/2;i++)
        {
            int j;
            for(j=1;j<=1000;j++)
                if(c[j]>=2)
                {
                    c[j]-=2;
                    a[n-i+1][n/2+1]=a[i][n/2+1]=j;
                    break;
                }
                if(j==1001) {End();return 0;}
            for(j=1;j<=1000;j++)
                if(c[j]>=2)
            {
                c[j]-=2;
                a[n/2+1][n-i+1]=a[n/2+1][i]=j;
                break;
            }
                if(j==1001) {End();return 0;}
        }
        int j;
        //确定正中间的那个数
        for(j=1;j<=1000;j++)
            if(c[j]>0) {a[n/2+1][n/2+1]=j;break;}
        if(j==1001) {End();return 0;}
        cout<<"YES"<<endl;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
                cout<<a[i][j]<<' ';
            cout<<endl;
        }
    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/xuzonghao/article/details/87953768