【算法笔记】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 ...
随机推荐
- 908D New Year and Arbitrary Arrangement
传送门 分析 代码 #include<iostream> #include<cstdio> #include<cstring> #include<string ...
- 954I Yet Another String Matching Problem
传送门 分析 我们先考虑暴力如何计算 对于S的子串SS,如果它有位置i使得SS[i] != T[i]那么我们就将两个字符之间用并查集连边 最后答案很明显就是并查集中所有边的个数 于是我们可以发现对于S ...
- .net正则查询
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- smarty内置函数、自定义函数
1.把字符串里的d字母替换成h格式:{'d'|str_replace:'h':$str}; d要查找的字符 h要替换的字符 $str字符串 2.function test($param){$p1=$p ...
- cef相关
chrome命令行参数:https://www.cnblogs.com/hushaojun/p/5981646.html cef启动调试,启动的命令行:--disable-web-security - ...
- kalilinux-权限提升
meterpeter: 可以在 Meterpreter 使用 incognito 来开始模拟过程: use incognito 展示 incognito 的帮助文档,通过输入 help 命令: hel ...
- python读写操作csv及excle文件
1.python读写csv文件 import csv #读取csv文件内容方法1 csv_file = csv.reader(open('testdata.csv','r')) next(csv_fi ...
- Maven项目常见错误
一.Cannot change version of project facet Dynamic Web Module to 3.0. 和 One or more constraints have n ...
- java状态模式
核心思想就是:当对象的状态改变时,同时改变其行为,很好理解!就拿QQ来说,有几种状态,在线.隐身.忙碌等,每个状态对应不同的操作,而且你的好友也能看到你的状态,所以,状态模式就两点:1.可以通过改变状 ...
- [LeetCode 题解]: Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...