徐子烊

  • 主页
  • 语雀
所有文章 关于我

徐子烊

  • 主页
  • 语雀

1105 Spiral Matrix (25 分)

2019-04-21

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 matrix is filled in from the first element at the upper-left corner, then move in a clockwise spiral. The matrix has m rows and n columns, 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:

1
2
12
37 76 20 98 76 42 53 95 60 81 58 93

Sample Output:

1
2
3
4
98 95 93
42 37 81
53 20 76
58 60 76

作者: CHEN, Yue

单位: 浙江大学

时间限制: 200 ms

内存限制: 64 MB

代码长度限制: 16 KB

题目大意

将一个数组由大到小排成一个螺旋数组。其中矩阵行与列的差要最小。

分析

首先计算出m,n,肯定大约是根号N的。从根号N开始,m逐渐增加,找到第一个n。如果n大于m,则交换。

然后用dir变量控制填入矩阵的方向。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
#include <deque>
#include <queue>
#include <map>
#include <set>
#include <cstring>
#include <cmath>
#include <sstream>

using namespace std;
int t, m, n;
vector<int> num;
vector<vector<int>> matrix;
int dirt[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
int main() {
cin >> t;
m = sqrt(t);
for (; m <= t; m++)
if (t % m == 0) {
n = t / m;
break;
}
if (n > m)//防止出现m=4,n=5这种情况
swap(n, m);
matrix.resize(m);
for(int i=0;i<m;i++)
matrix[i].resize(n);
num.resize(t);
for (int i = 0; i < t; i++)
cin>>num[i];
sort(num.begin(),num.end(),greater<int>());

int cnt=0,dir=0,x=0,y=0;
while(cnt<t){
matrix[x][y]=num[cnt++];
if((dir==0&&y==n-1)||(dir==0&&matrix[x][y+1]!=0))//向右到头,转下
dir=1;
else if((dir==1&&x==m-1)||(dir==1&&matrix[x+1][y]!=0))//向下到头,转左
dir=2;
else if((dir==2&&y==0)||(dir==2&&matrix[x][y-1]!=0))//向左到头,转上
dir=3;
else if((dir==3&&x==0)||(dir==3&&matrix[x-1][y]!=0))//向上到头,转右
dir=0;
x+=dirt[dir][0];
y+=dirt[dir][1];
}
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(j!=0)
cout<<" ";
cout<<matrix[i][j];
}
cout<<endl;
}
return 0;
}
  • PAT
  • PAT题解

扫一扫,分享到微信

微信分享二维码
1107 Social Clusters (30 分)
1103 Integer Factorization (30 分)
  1. 1. 1105 Spiral Matrix (25 分)
    1. 1.1. Input Specification:
    2. 1.2. Output Specification:
    3. 1.3. Sample Input:
    4. 1.4. Sample Output:
  2. 2. 题目大意
  3. 3. 分析
  4. 4. 代码
© 2020 徐子烊
Hexo Theme Yilia by Litten
  • 所有文章
  • 关于我

tag:

  • PAT
  • 多项式
  • dijkstra
  • 图
  • 树
  • 最大子序列
  • 进制转换
  • 二分
  • dfs
  • 连通分量
  • 模拟
  • STL
  • 大整数
  • 需复习
  • 链表
  • 贪心
  • 动态规划
  • 子序列
  • 二叉树
  • LCS
  • 栈
  • 排序
  • 质因数
  • 树状数组
  • 完全二叉树
  • 整数溢出
  • AVL树
  • 深搜
  • 哈希
  • bfs
  • 并查集
  • 红黑树
  • LCA
  • 拓扑排序
  • lca
  • 实习
  • bat
  • Leetcode
  • Java并发
  • 线程池
  • 6.824
  • 分布式
  • mapreduce
  • caffe
  • epoll
  • Raft
  • schnorr
  • 密码学
  • 设计模式
  • 单例模式
  • 原型模式
  • 工厂模式
  • 建造者模式
  • 抽象工厂
  • 桥接模式
  • 模版模式
  • 组合模式
  • 策略模式
  • 装饰器模式
  • 迭代器模式
  • 适配器模式

    缺失模块。
    1、请确保node版本大于6.2
    2、在博客根目录(注意不是yilia根目录)执行以下命令:
    npm i hexo-generator-json-content --save

    3、在根目录_config.yml里添加配置:

      jsonContent:
        meta: false
        pages: false
        posts:
          title: true
          date: true
          path: true
          text: false
          raw: false
          content: false
          slug: false
          updated: false
          comments: false
          link: false
          permalink: false
          excerpt: false
          categories: false
          tags: true
    

很惭愧

只做了一点微小的工作