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& ...
随机推荐
- Android拼图游戏的设计逻辑,从切图到交互动画,从关卡到倒计时,实例提高!
Android拼图游戏的设计逻辑,从切图到交互动画,从关卡到倒计时,实例提高! 群英传的最后一章,我大致的看了一下这个例子,发现鸿洋大神也做过,就参考两个人的设计逻辑,感觉都差不多,就这样实现起来了 ...
- Java集合之WeakHashMap
纸上得来终觉浅,绝知此事要躬行 --陆游 问渠那得清如许,为有源头活水来 --朱熹 WeakHashMap继承于AbstractMap,同时实现了Map接口. 和HashMap一样,Weak ...
- web报表工具FineReport常用函数的用法总结(文本函数)
文本函数 CHAR CHAR(number):根据指定数字返回对应的字符.CHAR函数可将计算机其他类型的数字代码转换为字符. Number:用于指定字符的数字,介于1Number:用于指定字符的数字 ...
- 最大的k个数问题
代码来源: http://blog.csdn.net/v_JULY_v 调整堆为小顶堆的代码片:基本思想就是把孩子节点中大的一个跟父节点交换 void HeapAdjust(int array[], ...
- java--加强之 jdk1.5简单新特性,枚举,注解
转载请申明出处:http://blog.csdn.net/xmxkf/article/details/9944041 Jdk1.51新特性(静态导入,可变参数,加强for循环,自动拆装箱) 08.ja ...
- WebService学习--(二)webservice相关介绍
一.WebService是什么? 1. 基于Web的服务:服务器端整出一些资源让客户端应用访问(获取数据) 2. 一个跨语言.跨平台的规范(抽象) 3. 多个跨平台.跨语言的应用间通信整合的方案(实际 ...
- Python list 两个不等长列表交叉合并
遇到一个需求,需要对两个长度不一定相等的列表进行交叉合并.像拉拉链一样(两边的拉链不一定相等). 如: a = [1, 3, 5] b = [2, 4, 6, 8] 需将a, b 合并为 c c = ...
- oracle to_date 函数
update pamsodt0p10 set cursysdate = to_date('2014-12-29 00:00:00','yyyy-mm-dd hh24:mi:ss') where cu ...
- maven技术(一)软件安装与配置
maven技术在研发的过程中,作为资源依赖管理非常出色,例如在Java项目开发过程中,需要各种各样jar包,一般情况下开发者会直接将所用到的jar包放在project的lib目录下,提供自己程序调用. ...
- HTMLConverter使用实例(转)
---- 本来,Applet的概念相当简单——只要在Web页面中加入一个< APPLET >标记就可以了.浏览器一遇到这个标记,就会下载对应的 Applet类文件,并启动自己的解释器运行这 ...