PAT_A1105#Spiral Matrix

Source:

PAT A1105 Spiral Matrix (25 分)

Description:

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×nmust be equal to N; mn; and mn 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 1. 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

Keys:

  • Simple simulation

Attention:

  • The basic idea is to sequence in descending order, sequentially filled matrix;
  • Direct open exceed 1e4 matrix memory, and m greater, the smaller the presence of n-sample, so instead of using a two-dimensional array of one-dimensional array
  • Four in the circulating loop required for determination pt <N

Code:

. 1  / * 
2  the Data: 2019-06-08 16:12:47
 . 3  Problem: Spiral # PAT_A1105 the Matrix
 . 4  the AC: 01:05:29
 . 5  
. 6  subject to the effect:
 7  sequence descending, clockwise into the matrix
 8  * / 
. 9 #include <Functional>
 10 #include <cstdio>
 . 11 #include <the cmath>
 12 is #include <algorithm>
 13 is  the using  namespace STD;
 14  const  int M + 1E4 = 10 ;
 15  int Matrix [M], A [M] ;
 16  
. 17  int main ()
 18 is  {
 . 19 #ifdef    ONLINE_JUDGE
20 #else
21     freopen("Test.txt", "r", stdin);
22 #endif
23 
24     int N,n,m;
25     scanf("%d", &N);
26     for(int i=0; i<N; i++)
27         scanf("%d", &a[i]);
28     for(int i=(int)sqrt((double)N); i>=1; i--)
29     {
30         if(N%i == 0)
31         {
32             n = i;
33             m = N/i;
34             break;
35         }
36     }
37     sort(a,a+N,greater<int>());
38     fill(matrix,matrix+M,0);
39     int pt=0,c=1,r=1,R=m,C=n;
40     while(pt < N)
41     {
42         for(int i=c; i<=n && pt<N; i++)
43             matrix[C*r-2+i-1]=a[pt++];
44         r++;
45         for(int i=r; i<=m && pt<N; i++)
46             matrix[C*i-2+n-1]=a[pt++];
47         n--;
48         for(int i=n; i>=c && pt<N; i--)
49             matrix[C*m-2+i-1]=a[pt++];
50         m--;
51         for(int i=m; i>=r && pt<N; i--)
52             matrix[C*i-2+c-1]=a[pt++];
53         c++;
54     }
55     for(int i=1; i<=R; i++)
56         for(int j=1; j<=C; j++)
57             printf("%d%c", matrix[C*i-2+j-1], j==C?'\n':' ');
58 
59 
60     return 0;
61 }

 

Guess you like

Origin www.cnblogs.com/blue-lin/p/10991319.html