1. 程式人生 > >PAT1105:Spiral Matrix

PAT1105:Spiral Matrix

end osi cas 其中 長度 put for input info

1105. Spiral Matrix (25)

時間限制 150 ms 內存限制 65536 kB 代碼長度限制 16000 B 判題程序 Standard 作者 CHEN, Yue

This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrixis filled in from the first element at the upper-left corner, then move in a clockwise spiral. The matrix has m

rows and ncolumns, 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

思路
題目要求將N個數轉換成 m*n 大小的矩陣形式,其中必須滿足:
1.m*n = N且滿足m-n最小(m >= n)
2.矩陣中的數按從大到小呈順時針向內螺旋的形式排列,類似一個漩渦一樣。

那麽有:
1.先將這組數按遞減排序。
2.暴力枚舉找出滿足題目要求1的m和n,構建矩陣二維數組
3.按照順時針遍歷矩陣,將數字一個個輸入進去
4.輸出。

註意:
1.構建矩陣時可以弄一堵"墻"保證遍歷不越界,另外走過的地方也算"墻"(即matrix[i][j] != -1)。
2.用一個數組go[4]表示遍歷的每一步(右下左上,順時針),每當遇到墻(matrix[i][j] != -1,要麽是INIT_MAX,要麽是之前走過的地方)時改變方向,如此循環。

代碼
#include<iostream>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;
/*
1.排序
2.找m、n
3.構建矩陣
4.輸出
*/
vector<vector<int>> go ={{0,1},{1,0},{0,-1},{-1,0}};//右下左上
const int INIT_MAX = pow(2,30);
bool cmp(const int a,const int b)
{
    return a > b;
}

int main()
{
    int N;
    while(cin >> N)
    {
        vector<int> num(N);
        for(int i = 0;i < N;i++)
        {
            cin >> num[i];
        }
        sort(num.begin(),num.end(),cmp);

        //find min(m - n)
        int m,n,curmin = INIT_MAX;
        for(int i = N;i >= sqrt(N);i--)
        {
            if(i * (N/i) == N && i - (N/i) < curmin)
            {
                 m = i;
                 n = N/i;
                 curmin = m - n;
            }
        }
        //build matrix
        vector<vector<int>> matrix(m + 2,vector<int>(n + 2,-1));
        for(int i = 0;i <= n + 1;i++)
        {
            matrix[0][i] = matrix[m + 1][i] = INIT_MAX;
        }
        for(int i = 0;i <= m + 1;i++)
        {
            matrix[i][0] = matrix[i][n + 1] = INIT_MAX;
        }
        int a = 1,b = 1,dir = 0;
        matrix[a][b] = num[0];
        for(int i = 1;i < num.size();i++)
        {
           if(matrix[a+go[dir][0]][b+go[dir][1]] != -1)
           {
              dir++;
              if(dir > 3)
                dir = 0;
           }
           a += go[dir][0];
           b += go[dir][1];
           matrix[a][b] = num[i];
        }
        //output
        for(int i = 1;i <= m;i++)
        {
            for(int j = 1;j <= n;j++)
            {
                if(j != 1)
                  cout << " ";
                cout << matrix[i][j];
            }
            cout << endl;
        }
    }
}

  

PAT1105:Spiral Matrix