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. tp5循环+判断

  2. vscode安装dlv插件报错:There is no tracking information for the current branch.

    vscode安装dlv插件报错:There is no tracking information for the current branch. https://blog.csdn.net/a7859 ...

  3. scp 的时候提示WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

    摘自:https://blog.csdn.net/haokele/article/details/72824847   @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...

  4. ORM对象/关系模型

    1 ORM 对象关系映射(ORM)提供了概念性的.易于理解的模型化数据的方法.ORM方法论基于三个核心原则: 简单:以最基本的形式建模数据. 传达性:数据库结构被任何人都能理解的语言文档化. 精确性: ...

  5. spring boot配置mybatis和事务管理

    spring boot配置mybatis和事务管理 一.spring boot与mybatis的配置 1.首先,spring boot 配置mybatis需要的全部依赖如下: <!-- Spri ...

  6. LoadRunner出现error问题及解决方法总结

    一.Step download timeout (120 seconds) 这是一个经常会遇到的问题,解决得办法走以下步骤:1.   修改run time setting中的请求超时时间,增加到600 ...

  7. kubernetes使用ceph

    一.有一个ceph cluster,假设已经准备好了,文档网上一大堆 二.开始集成ceph和kuberntes 2.1 禁用rbd features rbd image有4个 features,lay ...

  8. QT学习之常用类的总结

    QApplication 应用程序类 管理图形用户界面应用程序的控制流和主要设置       QPalate   QLabel 标签类 提供文本或者图像的显示   QPushButton 按钮类 提供 ...

  9. (转)走进AngularJs(六) 服务

    原文地址:http://www.cnblogs.com/lvdabao/p/3464015.html 今天学习了一下ng的service机制,作为ng的基本知识之一,有必要做一个了解,在此做个笔记记录 ...

  10. .NET基础 (01).NET基础概念

    .NET基础概念 1 什么是CTS.CLS和CLR2 开发和运行.NET程序需要的最基本环节是什么3 .NET是否支持多编程语言开发4 CLR技术和COM技术的比较5 什么是程序集和应用程序域 1 什 ...