【算法笔记】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 ...
随机推荐
- arping
强制交换机刷新MAC arping -I em2(网卡名称) 58.215.88.8(Vip)
- 使用dataview组件显示服务器端xml文件数据
来自<sencha touch权威指南>,约193页开始 ------------------------------------- (1)app.js代码: Ext.require([' ...
- pcl point merge
http://pointclouds.org/documentation/tutorials/pairwise_incremental_registration.php#pairwise-increm ...
- modifiers标量调节器插件的定义和使用
在插件目录plugins里新建文件 modifier.插件名.php文件(如 modifier.插件名.php)例:function smarty_modifier_test($utime, $for ...
- python 数据清洗
前言 1. 删除重复 2. 异常值监测 3. 替换 4. 数据映射 5. 数值变量类型化 6. 创建哑变量 统计师的Python日记[第7天:数据清洗(1)] 前言 根据我的Python学习计划: N ...
- CodeForces 540D Bad Luck Island (DP)
题意:一个岛上有石头,剪刀和布,规则就不用说了,问你最后只剩下每一种的概率是多少. 析:很明显的一个概率DP,用d[i][j][k]表示,石头剩下 i 个,剪刀剩下 j 个,布剩下 k 个,d[r][ ...
- Exception (1) Understanding Exception Handling
When an exception is thrown, it cannot be ignored--there must be some kind of notification or termin ...
- gitlab发送邮件配置
在使用gitlab过程中,通常需配置邮件来实现代码管理服务器向成员(member)发送邮件.本文将实现gitlab中邮件配置 1.编辑 /etc/gitlab/gitlab.rb,修改配置邮件,这里以 ...
- ARM汇编中一些重要伪指令
IMPORT ,定义表示这是一个外部变量的标号,不是在本程序定义的 EXPORT ,表示本程序里面用到的变量提供给其他模块调用的. 以上两个在汇编和C语言混合编程的时候用到 ENDP 表示PRO ...
- 新入门PGSQL数据库(尝试利用PGPOOL实现分布式),摘录笔记
概念: PostgreSQL (pronounced "post-gress-Q-L") is an open source relational database managem ...