PAT1105:Spiral Matrix
1105. Spiral Matrix (25)
This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasing order. A spiral matrixis filled in from the first element at the upper-left corner, then move in a clockwise spiral. The matrix has m rows and ncolumns, where m and n satisfy the following: m*n must be equal to N; m>=n; and m-n is the minimum of all the possible values.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N. Then the next line contains N positive integers to be filled into the spiral matrix. All the numbers are no more than 104. The numbers in a line are separated by spaces.
Output Specification:
For each test case, output the resulting matrix in m lines, each contains n numbers. There must be exactly 1 space between two adjacent numbers, and no extra space at the end of each line.
Sample Input:
12
37 76 20 98 76 42 53 95 60 81 58 93
Sample Output:
98 95 93
42 37 81
53 20 76
58 60 76 思路
题目要求将N个数转换成 m*n 大小的矩阵形式,其中必须满足:
1.m*n = N且满足m-n最小(m >= n)
2.矩阵中的数按从大到小呈顺时针向内螺旋的形式排列,类似一个漩涡一样。 那么有:
1.先将这组数按递减排序。
2.暴力枚举找出满足题目要求1的m和n,构建矩阵二维数组
3.按照顺时针遍历矩阵,将数字一个个输入进去
4.输出。 注意:
1.构建矩阵时可以弄一堵"墙"保证遍历不越界,另外走过的地方也算"墙"(即matrix[i][j] != -1)。
2.用一个数组go[4]表示遍历的每一步(右下左上,顺时针),每当遇到墙(matrix[i][j] != -1,要么是INIT_MAX,要么是之前走过的地方)时改变方向,如此循环。 代码
#include<iostream>
#include<vector>
#include<math.h>
#include<algorithm>
using namespace std;
/*
1.排序
2.找m、n
3.构建矩阵
4.输出
*/
vector<vector<int>> go ={{0,1},{1,0},{0,-1},{-1,0}};//右下左上
const int INIT_MAX = pow(2,30);
bool cmp(const int a,const int b)
{
return a > b;
} int main()
{
int N;
while(cin >> N)
{
vector<int> num(N);
for(int i = 0;i < N;i++)
{
cin >> num[i];
}
sort(num.begin(),num.end(),cmp); //find min(m - n)
int m,n,curmin = INIT_MAX;
for(int i = N;i >= sqrt(N);i--)
{
if(i * (N/i) == N && i - (N/i) < curmin)
{
m = i;
n = N/i;
curmin = m - n;
}
}
//build matrix
vector<vector<int>> matrix(m + 2,vector<int>(n + 2,-1));
for(int i = 0;i <= n + 1;i++)
{
matrix[0][i] = matrix[m + 1][i] = INIT_MAX;
}
for(int i = 0;i <= m + 1;i++)
{
matrix[i][0] = matrix[i][n + 1] = INIT_MAX;
}
int a = 1,b = 1,dir = 0;
matrix[a][b] = num[0];
for(int i = 1;i < num.size();i++)
{
if(matrix[a+go[dir][0]][b+go[dir][1]] != -1)
{
dir++;
if(dir > 3)
dir = 0;
}
a += go[dir][0];
b += go[dir][1];
matrix[a][b] = num[i];
}
//output
for(int i = 1;i <= m;i++)
{
for(int j = 1;j <= n;j++)
{
if(j != 1)
cout << " ";
cout << matrix[i][j];
}
cout << endl;
}
}
}
PAT1105:Spiral Matrix的更多相关文章
- [LeetCode] Spiral Matrix II 螺旋矩阵之二
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- [LeetCode] Spiral Matrix 螺旋矩阵
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
- 【leetcode】Spiral Matrix II
Spiral Matrix II Given an integer n, generate a square matrix filled with elements from 1 to n2 in s ...
- 【leetcode】Spiral Matrix II (middle)
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- 59. Spiral Matrix && Spiral Matrix II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- LeetCode:Spiral Matrix I II
Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...
- Java for LeetCode 059 Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- Leetcode#59 Spiral Matrix II
原题地址 相比于Spiral Matrix(参见这篇文章)要简单一些,因为是方阵,所以代码简洁一些. 注意当n是奇数的时候,中心小块要单独赋值(代码21行) 代码: vector<vector& ...
随机推荐
- Linux System Programming --Chapter Seven
文件和目录管理 一.文件与其元数据 我们首先看一下一个简单的文本文件是怎么保存的: 打开vim,编辑一段文本: [root@localhost ~]# vim hello.txt 编辑内容如下: op ...
- 【Java编程】Eclipse快捷键
Alt+左箭头,右箭头 以在编辑窗口切换标签 Alt+上下箭头, 以自动选择鼠标所在行,并将其上下移动 Ctrl+f6 ...
- Oracle EBS 重新编译无效对象 invalid object
1. 查看数据库中的无效对象 check oracle object SQL> select count(*) from dba_objects where status= ...
- LeetCode之“树”:Symmetric Tree && Same Tree
Symmetric Tree 题目链接 题目要求: Given a binary tree, check whether it is a mirror of itself (ie, symmetric ...
- Java-GenricServlet
public abstract class GenericServlet implements Servlet, ServletConfig, java.io.Serializable { priva ...
- 中国象棋游戏Chess(1) - 棋盘绘制以及棋子的绘制
本项目都使用QT来实现绘图,没有任何第三方的资源. 工程详情:Github 首先将棋盘设计为一个类Board // Board.h // Board类实现了棋盘的绘制以及显示 // #ifndef B ...
- OpenCV矩阵运算
矩阵处理 1.矩阵的内存分配与释放 (1) 总体上: OpenCV 使用C语言来进行矩阵操作.不过实际上有很多C++语言的替代方案可以更高效地完成. 在OpenCV中向量被当做是有一个维数为1的N维矩 ...
- ORALCE EBS ALERT 初体验
Oracle EBS Alert Alert 是一种Oracle系统中的一种机制,它可以监视系统数据库,在规定的情况下给规定用户一个通知,通知可以是邮件或者其他形式,在标注的系统和客户化系统中都是可以 ...
- Java + Selenium + TestNG + Maven
环境准备: 1. Java: Install Java jdk: Version: java 1.8 or aboveConfigure Java Environment Variables:Add ...
- 如何在Visual Studio 2017中使用C# 7+语法
前言 之前不知看过哪位前辈的博文有点印象C# 7控制台开始支持执行异步方法,然后闲来无事,搞着,搞着没搞出来,然后就写了这篇博文,不喜勿喷,或许对您有帮助. 在Visual Studio 2017配置 ...