【算法笔记】B1050 螺旋矩阵
本题要求将给定的 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 螺旋矩阵的更多相关文章
- PAT B1050 螺旋矩阵 (25 分)
本题要求将给定的 N 个正整数按非递增的顺序,填入“螺旋矩阵”.所谓“螺旋矩阵”,是指从左上角第 1 个格子开始,按顺时针螺旋方向填充.要求矩阵的规模为 m 行 n 列,满足条件:m×n 等于 N:m ...
- 【PAT】B1050 螺旋矩阵(25 分)
实在不觉得递归等方式有什么简单的地方,没错我就是用的最笨的方法模拟. 和我一样的小白看代码应该很容易理解. #include<stdio.h> #include<math.h> ...
- 【LeetCode-面试算法经典-Java实现】【059-Spiral Matrix II(螺旋矩阵II)】
[059-Spiral Matrix II(螺旋矩阵II)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given an integer n, generate a ...
- C++ 螺旋矩阵算法
清理磁盘空间的时候翻出了多年前写过的螺旋矩阵,代码效率和水平较低,纪念一下,保存到博客园! // ConsoleApplication3.cpp : 定义控制台应用程序的入口点. // #includ ...
- Java-基础编程(螺旋矩阵&乘法表)
package cn.rick.study; import java.io.BufferedReader;import java.io.InputStreamReader;import java.ut ...
- LeetCode 59. Spiral Matrix II (螺旋矩阵之二)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 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 ...
- HrbustOJ 1564 螺旋矩阵
Description 对于给定的一个数n,要你打印n*n的螺旋矩阵. 比如n=3时,输出: 1 2 3 8 9 4 7 6 5 Input 多组测试数据,每个测试数据包含一个整数n(1<=n& ...
- PAT 1105 Spiral Matrix[模拟][螺旋矩阵][难]
1105 Spiral Matrix(25 分) This time your job is to fill a sequence of N positive integers into a spir ...
随机推荐
- ORACLE 异机恢复
有时候需要将大的数据库发布到客户现场或转移机器时,不得不考虑在异机上恢复已经调整.测试好的库. dumpdp 全备的方法虽然易用,但在处理对象.索引.空间的时候异常的出错,比如:见有些公司,建表.索引 ...
- poj 2007 Scrambled Polygon
#include<stdio.h> #include<algorithm> using namespace std; #define Max 60 struct Point { ...
- Photo2
Story: 驯鹿:“其实我只是想要一个肩膀而已.” 小男孩:“当你需要我的时候,我会在你身边.” Profession: 页面的主色调是淡黄色,这种柔和的色调表达出了柔和的气氛,整个画面颜色的运用都 ...
- Perl 学习笔记-标量数据
最近学习Perl, 准备看一遍入门指南,关键的东西还是记录下来,以便以后复习和查看参考. 笔记来自<<Perl语言入门第5版>> 1. 在Perl内部,不区分整数值和浮点数值, ...
- 淘宝IP地址库
淘宝官方ip地址库 http://ip.taobao.com/ 接口说明 1. 请求接口(GET): http://ip.taobao.com/service/getIpInfo.php?ip=[ip ...
- 【小梅哥SOPC学习笔记】Altera SOPC嵌入式系统设计教程
Altera SOPC嵌入式系统设计教程 第1章 概述 SOPC(System On Programmable Chip,可编程的片上系统)是Altera公司提出来的一种灵活.高效的SOC解决方案.它 ...
- Sublime Text3下载激活汉化
一.下载 Sublime Text3下载 http://www.sublimetext.com/3 选择对应的版本下载即可. 二.激活 2.1 官网购买license key激活 2.2 网上查找各种 ...
- MVC4 Action 两种异步方式
1. xxxAsync/xxxCompleted 组合方式异步,xxxCompleted 就是他的回调函数,在执行完 xxxAsync 后调用xxxCompleted . 使用 异步方式必须继承A ...
- [Mac][转] ports命令
[Mac][转] ports命令 安装路径:/opt/local/lib/ 常用命令 port -d selfupdate #升级macport, 如同:cd /usr/ports && ...
- uwsgi启动报错WARNING: Can't find section "uwsgi" in INI configuration file autotestsite_uwsgi.ini
提示配置文件头部找不到 [uwsgi] 解决: 在最上方加上[uwsgi],有时候明明有标识,但是还是提示,那就再加一个