1050 螺旋矩阵 (25 分)
 

本题要求将给定的 N 个正整数按非递增的顺序,填入“螺旋矩阵”。所谓“螺旋矩阵”,是指从左上角第 1 个格子开始,按顺时针螺旋方向填充。要求矩阵的规模为 m 行 n 列,满足条件:m×n 等于 N;m≥n;且 m−n 取所有可能值中的最小值。

输入格式:

输入在第 1 行中给出一个正整数 N,第 2 行给出 N 个待填充的正整数。所有数字不超过 1,相邻数字以空格分隔。

输出格式:

输出螺旋矩阵。每行 n 个数字,共 m 行。相邻数字以 1 个空格分隔,行末不得有多余空格。

输入样例:

12
37 76 20 98 76 42 53 95 60 81 58 93

输出样例:

98 95 93
42 37 81
53 20 76
58 60 76

【碰壁法】判断下一个数,如果不为0就拐弯。如图:

  最后一个测试点总是超时,把 col = (int)sqrt(n) 改成  col = sqrt(n) 后 AC。

 #include<bits/stdc++.h>
using namespace std;
const int maxn = ;
int a[maxn][maxn] = {};
bool cmp(int a, int b){
return a > b;
}
int main(){
int n, ori[maxn];
cin>>n;
int col = sqrt(n), row;
while(n % col){
col--;
}
row = n / col;
for(int i = ; i < n; i++){
scanf("%d",&ori[i]);
}
sort(ori, ori + n, cmp);
int x = , y = , cnt = ;
a[y][x] = ori[cnt];
while(cnt + < n){
while(y + <= col && ! a[x][y + ])
a[x][++y] = ori[++cnt];
while(x + <= row && !a[x + ][y])
a[++x][y] = ori[++cnt];
while(y - > && !a[x][y - ])
a[x][--y] = ori[++cnt];
while(x - > && !a[x - ][y])
a[--x][y] = ori[++cnt];
}
for(int i=;i<=row;i++){
printf("%d",a[i][]);
for(int j=;j<=col;j++){
printf(" %d",a[i][j]);
}
printf("\n");
}
return ;
}

【算法笔记】B1050 螺旋矩阵的更多相关文章

  1. PAT B1050 螺旋矩阵 (25 分)

    本题要求将给定的 N 个正整数按非递增的顺序,填入“螺旋矩阵”.所谓“螺旋矩阵”,是指从左上角第 1 个格子开始,按顺时针螺旋方向填充.要求矩阵的规模为 m 行 n 列,满足条件:m×n 等于 N:m ...

  2. 【PAT】B1050 螺旋矩阵(25 分)

    实在不觉得递归等方式有什么简单的地方,没错我就是用的最笨的方法模拟. 和我一样的小白看代码应该很容易理解. #include<stdio.h> #include<math.h> ...

  3. 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】

    [059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...

  4. C++ 螺旋矩阵算法

    清理磁盘空间的时候翻出了多年前写过的螺旋矩阵,代码效率和水平较低,纪念一下,保存到博客园! // ConsoleApplication3.cpp : 定义控制台应用程序的入口点. // #includ ...

  5. Java-基础编程(螺旋矩阵&乘法表)

    package cn.rick.study; import java.io.BufferedReader;import java.io.InputStreamReader;import java.ut ...

  6. LeetCode 59. Spiral Matrix II (螺旋矩阵之二)

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

  7. LeetCode 54. Spiral Matrix(螺旋矩阵)

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

  8. HrbustOJ 1564 螺旋矩阵

    Description 对于给定的一个数n,要你打印n*n的螺旋矩阵. 比如n=3时,输出: 1 2 3 8 9 4 7 6 5 Input 多组测试数据,每个测试数据包含一个整数n(1<=n& ...

  9. PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]

    1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...

随机推荐

  1. linux环境下搭建osm_web服务器四(对万国语的地名进行翻译和检索):

    对万国语的地名进行翻译和检索 经过 前三篇的调试,已经有了一个完整的Map可以浏览,我们痛苦的世界范围数据下载.导入过程也结束了.要提醒一下的是,鉴于网速,不要下载 planetosm.lastest ...

  2. mosquitto ---配置SSL/TLS linux

    mosquitto ---配置SSL/TLS 摘自: https://www.cnblogs.com/saryli/p/9821343.html 在服务器电脑上面创建myCA文件夹, 如在/home/ ...

  3. HBase批量插入的简单代码

    由于项目需要从HBase里读取数据,进行MapReduce之后输出到HDFS中. 为了测试方便,我这里写了一个批量插入HBase数据的测试代码.采用的Maven工程. 打算,今后的所有用到的小测试例子 ...

  4. MapReduce的初次尝试

    ====前提: 搭建好集群环境(zookeeper.hadoop.hbase). 搭建方法这里就不进行介绍了,网上有很多博客在介绍这些. ====简单需求: WordCount单词计数,号称Hadoo ...

  5. 4619 Warm up 2

    #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; ][]; ...

  6. STL中 map 和 multimap

    1. 所在头文件<map>. 命名空间std, 声明如下: namespace std{ template <class Key,class T, class Compare = l ...

  7. mybaits foreach

    <select id="selectQuickConsultDoctorList" resultMap="BaseResultMap" parameter ...

  8. 团体程序设计天梯赛L1-024 后天 2017-03-22 17:59 68人阅读 评论(0) 收藏

    L1-024. 后天 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 如果今天是星期三,后天就是星期五:如果今天是星期六,后天就 ...

  9. 学习python4

    文件系统实现文件的增删改查 UnicodeDecodeError: 'gbk' codec can't decode byte 0x9a in position 8: illegal multibyt ...

  10. elasticsearch常用JAVA API 实例

    1.引入dependency <dependency> <groupId>org.springframework.data</groupId> <artifa ...