PAT甲級1105 Spiral Matrix (25分) 測試點分析 旋轉填充矩陣,做過很多次了

1105 Spiral Matrix (25分)
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 10
​4
​​ . 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

測試點 3 4 5都是N=1 2 3的情況,自己輸出一下測試試試

然後思路嘛,關鍵就是實現迴旋地在矩陣賦值

按照下面的圖來
在這裏插入圖片描述
首先確定了行數爲row, 列數爲clolumn
旋轉的第一圈了黑色標記的1 2 3 4 旋轉的第二圈填充了5 6 7 。
填充也就四種形式,先上 然後右 然後下 然後左 就是一圈了。

首先看填充四種形式第一種: 上方的填充,即第一圈 黑色1 和 第二圈的黑色5, 他們都是一個循環填充的,我們找找規則,如果以time表示第幾圈,那麼就是 for(int n=time; n<column-time; n++) 賦值是matrix[time][n]=sn[pos++];

同理四種填充的第二種:右側填充,即第一圈的黑色2 和第二圈的黑色 6 ,
找找規則就是
for(int n=time+1; n<row-time; n++)
{
matrix[n][column-1-time]=sn[pos++];
}
同理剩下的也可找到規則

AC代碼

#include <iostream>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <math.h>
using namespace std;
int cmp(int a,int b)
{
    return a>b;
}

int main(void)
{
    int N;
    cin>>N;
    int row,column;
    if(N==1)
    {
        row=1;
        column=1;
    }
    else if(N==2)
    {
        row=2;
        column=1;
    }
    else if(N==3)
    {
        row=3;
        column=1;
    }
    else
        for(int i=sqrt(N); i>=1; i--)
        {
            if(N%i==0)
            {
                row=N/i;
                column=i;
                break;
            }
        }
    int sn[N];
    for(int i=0; i<N; i++)
    {
        cin>>sn[i];
    }
    int pos=0;
    sort(sn,sn+N,cmp);
    int matrix[row][column];

    int time=0;
    while(pos<N)
    {

        for(int n=time; n<column-time&&pos<N; n++)
        {
            matrix[time][n]=sn[pos++];
        }
        if(pos==N)
            break;

        for(int n=time+1; n<row-time&&pos<N; n++)
        {
            matrix[n][column-1-time]=sn[pos++];
        }
        if(pos==N)
            break;

        for(int n=column-time-2; n>=time&&pos<N; n--)
        {
            matrix[row-1-time][n]=sn[pos++];
        }
        if(pos==N)
            break;


        for(int n=row-1-time-1; n>time&&pos<N; n--)
        {
            matrix[n][time]=sn[pos++];
        }
        if(pos==N)
            break;
        time++;
    }

    for(int m=0; m<row; m++)
    {
        for(int n=0; n<column; n++)
        {
            if(n==0)
            {
                cout<<matrix[m][n];
            }
            else
                cout<<' '<<matrix[m][n];
        }
        cout<<endl;
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章