PAT1105:Spiral Matrix

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;
        }
    }
}

  

时间: 2024-07-30 15:29:08

PAT1105:Spiral Matrix的相关文章

Spiral Matrix(LintCode)

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. Example Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 难得的一次AC! 虽然感觉题

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

LeetCode:Spiral Matrix - 螺旋输出矩阵中的元素

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix/ 3.题目内容 英文:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 中文:给出一个m行n列的矩阵,以螺旋顺序返回矩阵中的所有元素. 例如:现有矩阵如下: [  [ 1,

[LeetCode][Java] Spiral Matrix II

题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题意: 给定一个整数 n,生成一个正方形矩阵.矩阵中包含着从1到n2 这些元素,并且

[LeetCode]59.Spiral Matrix II

[题目] Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] [分析] 模拟 [代码] /**-------------------------

【leetcode】Spiral Matrix

Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. For example,Given the following matrix: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] You should return [1,2,3,6,9,8,7,4,5]. 如果下一步会遇到访问

leetcode Spiral Matrix II

题目:是Spiral Matrix相关的的.这题的意思是给定一个n,那么在n*n的矩阵里按照循环记录将1,2,3,..., n*n.如下如果给定3,那么: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]一开始我想是不是有数学公式直接下标对应的,那直接遍历输出就可以了.但是推了一会,没有什么好的头绪.于是就分情况讨论了,和Spiral Matrix一样的分情况,只是这里的分情况是记录在数组里面.利用上,下,左,右,分别记录每次循环可以到达的四个边界,我们每次就输

每日算法之四十一:Spiral Matrix II (螺旋矩阵)

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 针对这个问题采用最直观的方式即可,即螺旋插入,这里有两个地方需要注意,一个是插入边界的界定,

【LeetCode】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]此题与Spiral Matrix类似,可以用相同的方法解决,相比之下,此题比前一题简单 publ