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 matrixvector<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];}//outputfor(int i = 1;i <= m;i++){for(int j = 1;j <= n;j++){if(j != 1)cout << " ";cout << matrix[i][j];}cout << endl;}}
}

  

转载于:https://www.cnblogs.com/0kk470/p/7911301.html

PAT1105:Spiral Matrix相关推荐

  1. 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 ...

  2. LeetCode 59 Spiral Matrix II(螺旋矩阵II)(Array)

    版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/5214 ...

  3. LeetCode Spiral Matrix II (生成螺旋矩阵)

     Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. F ...

  4. 59. Spiral Matrix II

    /** 59. Spiral Matrix II * 12.5 by Mingyang* 注意,这里我们说的Matrix就是正方形,不再是长方形了,所以我们会用* 更简单的方法,就是直接上下左右分别加 ...

  5. Spiral Matrix I II

    Spiral Matrix I Given an integer n, generate a square matrix filled with elements from 1 to n^2 in s ...

  6. C#LeetCode刷题之#59-螺旋矩阵 II(Spiral Matrix II)

    目录 问题 示例 分析 问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3678 访问. 给定一个正整数 n,生成一 ...

  7. C#LeetCode刷题之#54-螺旋矩阵(Spiral Matrix)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3672 访问. 给定一个包含 m x n 个元素的矩阵(m 行, ...

  8. LeetCode 59. Spiral Matrix II

    59. Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 ...

  9. LeetCode 54. Spiral Matrix

    54. Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the m ...

最新文章

  1. Asp.net 1.0 升级至 ASP.NET 2.0十个问题总结
  2. 《大数据系统基础》课程实践项目中期答辩顺利举行,清华持续探索大数据人才教育创新之路
  3. 根据应用程序池名称获得第一个站点的物理路径
  4. eureka源码:开启服务注册
  5. mysql外键_mysql系列之存储引擎
  6. 自然语言交流系统 phxnet团队 创新实训 项目博客 (五)
  7. [转]ES7、ES8、ES9、ES10新特性大盘点
  8. 从抛硬币试验看随机游走定义的基本概念错误
  9. 重磅公开!36个高考数学破题大招
  10. 为什么析构函数可以能声明为虚函数,构造函数不可以
  11. es6 数组合并_JavaScript学习笔记(十九)-- ES6
  12. 深度学习中防止过拟合的方法
  13. android 底部黑边,android – 截屏周围的黑色边缘
  14. Spark 性能相关参数配置详解-Storage篇
  15. C++ 引用计数技术简介(1)
  16. 解决firefox、chrome不兼容cursor:hand 设置鼠标为手型的方法
  17. 人大金仓数据库高可用集群部署教程
  18. 谷歌和金山词霸合作 翻译行业垄断出现
  19. React项目本地环境正常显示,打包部署服务器图片不显示问题
  20. 用python 判断一个单链表是否有环

热门文章

  1. Please make sure you have the correct access rights and the repository exists
  2. 计算机应用教程第9,计算机应用教程(第8版)第六章
  3. c++自由读写配置ifstream(一)
  4. 人脸识别签到_矿区签到出“新招” 人脸识别考勤上线
  5. Intel处理器CPUID指令学习
  6. chechbox 的颜色android,Android 之 CheckBox 详解
  7. 【scala】IDEA运行scala程序:Error:scalac: bad option: ‘-make:transitive‘
  8. 【Elasticsearch】搭建Elasitc stack集群需要注意的日志问题
  9. 【Redis】redis 主从复制
  10. 【es】Elasticsearch:inverted index,doc_values及source