1.回旋矩阵,主要是给出一个数组,先对数组进行降序排序,使用sort和重写比较函数,然后填充到回旋矩阵中。
2.回旋矩阵填充思路:先建立一个m*n的所有值为-1的矩阵,-1表示数值没被填充,
(1)一开始往右填充,当右边到达边界值或者右边的数已经被填充,就改为往下填充;
(2)往下填充遇到边界值或者下边的数已经被填充,则往左填充;
(3)往左填充遇到边界值或者左边的数已经被填充,改为往上走;
(4)往上填充遇到边界值或者上边的数已经被填充,则往右填充,回到(1)。
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
AC代码:
[c language=”++”]
//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
bool cmp(const int&a, const int&b)
{
return a > b;
}
int main(void)
{
int total;
scanf("%d", &total);
vector<int> num(total, 0);
for (int i = 0; i < total; i++)
{//读取数据
scanf("%d", &num[i]);
}
//进行排序
sort(num.begin(), num.end(), cmp);
int matM, matN;
int minDiff = INT_MAX;
//求出m>=n且abs(m-n)最小时,m与n的取值
for (int i = 1; i <= sqrt((double)total) + 1; i++)
{
if (i*(total / i) == total&&abs(i – (total / i)) < minDiff)
{//如果符合要求,且差比上次的小,则进行更新
matM = i;
matN = total / i;
minDiff = abs(matM – matN);
}
}
if (matM < matN)
swap(matM, matN);
//读取的都是正数,利用-1来表示还没填充
vector<vector<int>>mat(matM, vector<int>(matN, -1));
int direction = 0;//方向,0表示往右,1表示往下,2表示往左,3表示往上
int m = 0, n = 0;//m,n表示上次填充的位置
for (int i = 0; i < total; i++)
{
mat[m][n] = num[i];
if (direction == 0)
{//往右走
if (n == matN – 1 || mat[m][n + 1] != -1)
{//如果遇到边界,或者右边的位置已经填充数值,则往下走
direction = 1;
m++;
}
else//继续往右走
n++;
}
else if (direction == 1)
{//往下走
if (m == matM – 1 || mat[m + 1][n] != -1)
{//如果遇到边界,或者下边的位置已经填充数值,则往左走
direction = 2;
n–;
}
else//继续往下走
m++;
}
else if (direction == 2)
{//往左走
if (n == 0 || mat[m][n – 1] != -1)
{//如果遇到边界,或者左边的位置已经填充数值,则往上走
direction = 3;
m–;
}
else//继续往左走
n–;
}
else if (direction == 3)
{//往上走
if (m == 0 || mat[m – 1][n] != -1)
{//如果遇到边界,或者上边的位置已经填充数值,则往右走
direction = 0;
n++;
}
else//继续往上走
m–;
}
}
for (int i = 0; i < matM; i++)
{
for (int j = 0; j < matN; j++)
{
printf("%d", mat[i][j]);
if (j != matN – 1)
printf(" ");
}
printf("\n");
}
return 0;
}
[/c]