首页 > 其他分享 >1105 Spiral Matrix

1105 Spiral Matrix

时间:2022-11-19 02:22:28浏览次数:37  
标签:matrix int Spiral spiral 1105 each line 105 Matrix

This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The matrix has m rows and n columns, where m and n satisfy the following: m×n must be equal to N; m≥n; and m−n is the minimum of all the possible values.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N. Then the next line contains N positive integers to be filled into the spiral matrix. All the numbers are no more than 104. The numbers in a line are separated by spaces.

Output Specification:

For each test case, output the resulting matrix in m lines, each contains n numbers. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line.

Sample Input:

12
37 76 20 98 76 42 53 95 60 81 58 93
 

Sample Output:

98 95 93
42 37 81
53 20 76
58 60 76

#include<stdio.h>
#include<algorithm>

using namespace std;

int seq[10005];
bool seqbool[105][105];
int out[105][105];

bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int num;
    scanf("%d",&num);
    for(int i=0;i<num;i++)
    {
        scanf("%d",&seq[i]);
    }
    sort(seq,seq+num,cmp);
    int min=num;
    int m=num;
    int n=1;
    for(int i=1;i<=num;i++)  //i为行 
    {
        if(num%i==0)
        {
            int j=num/i;
            if(i>=j&&((i-j)<=min))
            {
                m=i;
                n=j;
                min=i-j;
            }
        }
    }
    int tempm=0;
    int tempn=0;
    int i=0;
    int prority=1;
    while(1)
    {
        if(seqbool[tempm][tempn]!=true)
        {
            out[tempm][tempn]=seq[i];
            i++;
            seqbool[tempm][tempn]=true;
        
        }
        else
        {
            
            if(tempn!=n-1&&seqbool[tempm][tempn+1]==false) 
            {
                if(prority!=1)
                {
                    if(prority==2)
                    {
                        if(tempm!=m-1&&seqbool[tempm+1][tempn]==false) 
                        {
                            tempm++;
                            prority==2;
                        }
                        else
                        {
                        tempn++;
                        prority=1;
                        }
                    }
                    if(prority==3)
                    {
                        if(tempn!=0&&seqbool[tempm][tempn-1]==false) 
                        {
                            tempn--;
                            prority=3;
                        }
                        else
                        {
                        tempn++;
                        prority=1;
                        }
                    }
                    if(prority==4)
                    {
                        if(tempm!=0&&seqbool[tempm-1][tempn]==false)
                        {
                            tempm--;
                            prority=4;
                        }
                        else
                        {
                            tempn++;
                            prority=1;
                        }
                    }
                }
                else
                {
                    tempn++;
                    prority=1;
                }
            
            }
            else if(tempm!=m-1&&seqbool[tempm+1][tempn]==false)
            {
                    if(prority!=2)
                {
                    if(prority==1)
                    {
                        if(tempn!=n-1&&seqbool[tempm][tempn+1]==false) 
                        {
                                tempn++;
                                prority=1;
                        }
                        else
                        {
                                tempm++;
                                prority=2;
                        }
                    }
                    if(prority==3)
                    {
                        if(tempn!=0&&seqbool[tempm][tempn-1]==false) 
                        {
                            tempn--;
                            prority=3;
                        }
                        else
                        {
                            tempm++;
                            prority=2;
                        }
                    }
                    if(prority==4)
                    {
                        if(tempm!=0&&seqbool[tempm-1][tempn]==false)
                        {
                            tempm--;
                            prority=4;
                        }
                        else
                        {
                            tempm++;
                            prority=2;
                        }
                    }
                }
                else
                {
                    tempm++;
                prority=2;
                }
                
            }
            else if(tempn!=0&&seqbool[tempm][tempn-1]==false)
            {
                tempn--;
                prority=3;
                
            }
            else if(tempm!=0&&seqbool[tempm-1][tempn]==false)
            {
                tempm--;
                prority=4;
            }
            
        
        }
        if(i==num) break;
    }
    for(int t=0;t<m;t++)
    {
        for(int j=0;j<n;j++)
        {
            printf("%d",out[t][j]);
            if(j!=n-1) printf(" ");
        }
        if(t!=m-1) printf("\n");
    }
}

 

 

标签:matrix,int,Spiral,spiral,1105,each,line,105,Matrix
From: https://www.cnblogs.com/zzzlight/p/16905365.html

相关文章

  • bzoj #4161. Shlw loves matrixI 常系数线性递推
    常系数线性递推题目链接做法:容易得到一个\(k^3logn\)的做法,考虑更快。需要利用矩阵特征方程f(x)(恰好前些天刚学对\(A^n\)用\(f(A)\)可以对其取模。之后就可以快速求......
  • 73. Set Matrix Zeroes
    ivena m x n matrix,ifanelementis0,setitsentirerowandcolumnto0.Doitinplace.clicktoshowfollowup.//如果某个元素为0,则把该元素所在的行和......
  • AtCoder Beginner Contest 277 F Sorting a Matrix
    SortingaMatrix拓扑序首先可以明确无论怎么交换行列,原本在同一行或者同一列的元素,还是会处于同一行或者同一列条件一每行每行地看,如果能满足从小到大的条件,说明第......
  • 中文书籍对《人月神话》的引用(20211105更新161-165本):大师品软件、JavaScript开发框架
    ​​中文书籍对《人月神话》的引用(第001到160本)>>​​《人月神话》于1975年出版,1995年出二十周年版。自出版以来,该书被大量的书籍和文章引用,直到现在热潮不退。UMLChina摘录......
  • Magic Matrix
    传送门自己low思路:发现\(i,j,k\)可以随意互换,那么随意互换后容易得到\(a_i,a_j,a_k\)中最大值和次大值(不严格)一定相等。那么枚举\(i\),可以发现若\(a_{ij}<a_{i......
  • 马扎克全系列机床数据采集 Smooth C, Smooth G,Smooth X,Smart, Matrix Nexu2, 640MT,
     internalstaticexternintMazConnect(refushortparam0,[MarshalAs(UnmanagedType.LPStr),In]stringparam1,intparam2,intparam3);[DllImport("NTIFDLL")......
  • LeetCode_Array_73. Set Matrix Zeroes (C++)
    目录​​1,题目描述​​​​2,思路​​​​3,代码【C++】​​​​4,测试效果​​1,题目描述Givenamxnmatrix,ifanelementis0,setitsentirerowandcolumnto0.Do......
  • Normal Matrix(法向量变换矩阵)
    我们都知道gl的坐标系统。它的工作是将坐标从一个坐标系转到另一个坐标系。其中我们用到了几个转换矩阵。其中最为重要的是模型(Model)、视图(View)、投影(Projection)三个矩阵。......
  • LeetCode 54. Spiral Matrix
    ​​题目​​水题classSolution{public:vector<int>spiralOrder(vector<vector<int>>&matrix){inti=0,j=0;vector<int>ans;int......
  • 1105 链表合并(JAVA)
    给定两个单链表L1=a1→a2→⋯→an−1→an和L2=b1→b2→⋯→bm−1→bm。如果n≥2m,你的任务是将比较短的那个链表逆序,然后将之并入比较长的那个链表,得到一个形如a1→a2→......