PAT1105:Spiral Matrix
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 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的更多相关文章
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [LeetCode] Spiral Matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 59. Spiral Matrix && Spiral Matrix II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- LeetCode:Spiral Matrix I II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- Java for LeetCode 059 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- Leetcode#59 Spiral Matrix II
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
随机推荐
- PSAM 卡的应用 操作方法
PSAM 卡的应用 PSAM 功能 终端安全存储模块 PASM 常用于 脱机交易的 安全认证 脱机交易的流程 1.卡片对持卡人的认证(防止 ...
- platform_driver_probe与platform_driver_register的区别
Platform Device and Drivers 从<linux/platform_device.h>我们可以了解Platform bus上面的驱动模型接口:platform_de ...
- saiku运行时报错max_length_for_sort_data 需要set higher
infiniDB或者mysql数据库,运行时,按某个字段排序会出错.报错:max_length_for_sort_data ... set higher. saiku报错, 也是这样. 这是数 ...
- RedHat系列软件管理(第二版) --二进制软件包管理
RedHat系列软件管理 --二进制软件包管理 Linux学习思想-Linux相对与Windows来非常透明,因此,无论是系统,还是软件,都会有本身自带,或者是Man给提供的非常详细的说明/帮助文档, ...
- C语言之可变参实现scanf函数
既然有printf函数可变参实现,那就一定有scanf函数的可变参实现.废话不多说,源码奉上: 本源码不过多分析,如要明白原理,请翻本博客以往的文章看说明. 欢迎关注新浪微博:http://weibo ...
- HBase Compaction
当 client 向 hregion 端 put() 数据时, HRegion 会判断当前的 memstore 的大小是否大于参数hbase.hregion.memstore.flush.size 值 ...
- 基于condition 实现的线程安全的优先队列(python实现)
可以把Condiftion理解为一把高级的琐,它提供了比Lock, RLock更高级的功能,允许我们能够控制复杂的线程同步问题.threadiong.Condition在内部维护一个琐对象(默认是RL ...
- 一种WPF在后台线程更新UI界面的简便方法
WPF框架规定只有UI线程(主线程)可以更新界面,所有其他后台线程无法直接更新界面.幸好,WPF提供的SynchronizationContext类以及C#的Lambda表达式提供了一种方便的解决方法 ...
- Supervisor安装、配置、开启启动
1.安装Python包管理工具(easy_install) wget --no-check-certificate https://bootstrap.pypa.io/ez_setup.py -O - ...
- char 与 String 相等比较
这是一个相当2 相当基础 相当没有意义的帖子:但今天因为这个问题引发了一个bug.小细节也很重要!!! char a='1'; // char b='2dsf'; //cha ...