Source:

PAT A1105 Spiral Matrix (25 分)

Description:

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

Keys:

  • 简单模拟

Attention:

  • 基本思路就是把序列按照从大到小的顺序,依次填入矩阵中;
  • 直接开1e4的矩阵会超出内存,而且样例中存在m较大,n较小的情况,因此用一维数组代替二维数组
  • 循环内的四个for循环需要判断pt<N

Code:

 /*
Data: 2019-06-08 16:12:47
Problem: PAT_A1105#Spiral Matrix
AC: 01:05:29 题目大意:
序列从大到小,顺时针放入矩阵中
*/
#include<functional>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int M=1e4+;
int matrix[M], a[M]; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int N,n,m;
scanf("%d", &N);
for(int i=; i<N; i++)
scanf("%d", &a[i]);
for(int i=(int)sqrt((double)N); i>=; i--)
{
if(N%i == )
{
n = i;
m = N/i;
break;
}
}
sort(a,a+N,greater<int>());
fill(matrix,matrix+M,);
int pt=,c=,r=,R=m,C=n;
while(pt < N)
{
for(int i=c; i<=n && pt<N; i++)
matrix[C*r-+i-]=a[pt++];
r++;
for(int i=r; i<=m && pt<N; i++)
matrix[C*i-+n-]=a[pt++];
n--;
for(int i=n; i>=c && pt<N; i--)
matrix[C*m-+i-]=a[pt++];
m--;
for(int i=m; i>=r && pt<N; i--)
matrix[C*i-+c-]=a[pt++];
c++;
}
for(int i=; i<=R; i++)
for(int j=; j<=C; j++)
printf("%d%c", matrix[C*i-+j-], j==C?'\n':' '); return ;
}

PAT_A1105#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. N天学习一个Linux命令之hostnamectl

    前言 安装了CentOS7,发现按照以前修改文件/etc/sysconfig/network HOSTNAME字段主机名的方式不生效了,查资料发现可以使用hostnamectl命令 用途 Contro ...

  2. [转]十五天精通WCF——第十一天 如何对wcf进行全程监控

    说点题外话,我们在玩asp.net的时候,都知道有一个叼毛玩意叫做“生命周期”,我们可以用httpmodule在先于页面的page_load中 做一些拦截,这样做的好处有很多,比如记录日志,参数过滤, ...

  3. HPC2013小节

    对于高性能计算,三个分支能耗.高性能.容错.下面我对会议的主要内容作一个小节,很多问题也是不求甚解. 下面针对大会内容,我主要总结如下,会有了解不周的地方,欢迎讨论:大会主要报告分成3个方向,1.基础 ...

  4. ubuntu设置不睡眠的方法

    Ubuntu系统不睡眠设置方法:要完成这个设置Ubuntu有两种方法:第一种是打开 System Settings –> Power,中文版是打开系统设置 -> 电源),然后进行设置.但不 ...

  5. HDU 4514

    真是神奇,G++TLE,C++500MS... 判环有一个图论知识就是,m>=n时必有环.如果以m的范围建图,会MLE. 然后,利用拓扑排序再来判定是否有环,因为有些景点可能是孤立的.同时,在拓 ...

  6. light oj1074

    Description The people of Mohammadpur have decided to paint each of their houses red, green, or blue ...

  7. com关于引用计数

    实现引用计数并不难,但在什么层次上进行引用计数呢? 依照com规范,一个com组件能够实现多个com对象.而且每一个com对象又能够支持多个com接口,这样的层次结构为我们实现引用计数提供了多种选择方 ...

  8. 虚拟机中的ip和本机的ip不是一个网段的

    将虚拟机的网络适配器 模式改为桥接模式 然后就会和主机处于同一个网段了

  9. B1060 [ZJOI2007]时态同步 dfs

    两遍dfs,第一遍有点像找重链,第二遍维护答案,每个点维护一个当前深度,然后就没啥了. ps:memset(lst,-1,sizeof(lst));这一句多余的话让我debug半天... 题干: De ...

  10. bzoj2503 相框——思路

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2503 思路题: 首先,这种问题应该注意到答案只跟度数有关,跟其他什么连接方法之类的完全无关: ...