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的更多相关文章

  1. [LeetCode] Spiral Matrix II 螺旋矩阵之二

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

  2. [LeetCode] Spiral Matrix 螺旋矩阵

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  3. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

  4. 【leetcode】Spiral Matrix II

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

  5. 【leetcode】Spiral Matrix II (middle)

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

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

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

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

  9. Leetcode#59 Spiral Matrix II

    原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...

随机推荐

  1. 对C语言中递归算法的分析

    C通过运行时堆栈支持递归函数的实现.递归函数就是直接或间接调用自身的函数.     许多教科书都把计算机阶乘和菲波那契数列用来说明递归,非常不幸我们可爱的著名的老潭老师的<C语言程序设计> ...

  2. Java集合之Set

    Set也是继承自Collection,Set也是集合的一种,同时Set不允许重复的元素存在.Set的实现类都是基于Map来实现的,其中HashSet是通过HashMap来实现的,TreeSet是通过T ...

  3. LeetCode之“散列表”:Isomorphic Strings

    题目链接 题目要求: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic i ...

  4. Gradle 1.12用户指南翻译——第二十二章. 标准的 Gradle 插件

    其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Github上的地址: https://g ...

  5. 【Visual C++】游戏编程学习笔记之七:键盘输入消息

     本系列文章由@二货梦想家张程 所写,转载请注明出处. 作者:ZeeCoder  微博链接:http://weibo.com/zc463717263 我的邮箱:michealfloyd@126.c ...

  6. 【Qt编程】基于QWT的曲线绘制及图例显示操作

    在<QWT在QtCreator中的安装与使用>一文中,我们完成了QWT的安装,这篇文章我们讲讲基础曲线的绘制功能. 首先,我们新建一个Qt应用程序,然后一路默认即可.这时,你会发现总共有: ...

  7. 菜鸟玩云计算之十九:Hadoop 2.5.0 HA 集群安装第2章

    菜鸟玩云计算之十九:Hadoop 2.5.0 HA 集群安装第2章 cheungmine, 2014-10-26 在上一章中,我们准备好了计算机和软件.本章开始部署hadoop 高可用集群. 2 部署 ...

  8. ORACLE ERP各模块会计分录

      ORACLE ERP各模块会计分录   第一章 采购模块 一.资产采购(科目来源:库存组织) 1.物料接收 借  材料采购     接收数量*采购单价 贷 应计暂估     接收数量*采购单价 2 ...

  9. ruby rails_autolink不能加载的原因

    从rails 3.1.0开始,默认在ActionView::Helper::TextHelper中的auto_link方法已经被移除,放到了第三方的gem里:rails_autolink.遂想试一下其 ...

  10. Webapck项目开发基本构建及配置

    1.创建项目文件夹 myapp 手动创建myapp,或mkdir myapp 2.cd myapp 3.npm init (初始化项目) 4.一路回车(关于项目信息的填写,可以不写,一路回车即可) 可 ...