PAT (Advanced Level) Practise 1105 Spiral Matrix (25)
来源:互联网 发布:天猫好还是淘宝好 编辑:程序博客网 时间:2024/05/17 07:26
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 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:1237 76 20 98 76 42 53 95 60 81 58 93Sample Output:
98 95 9342 37 8153 20 7658 60 76
题意:将N个数从大到小按蛇形的方式放入m行n列的矩阵,并且m*n=N,m>n,m-n最小
解题思路:先从小到大排序,然后按蛇形的方向(右下左上)填入矩阵
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <queue>#include <vector>#include <set>#include <stack>#include <map>#include <climits>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;int a[10005],ans[1000][1000];int n, r, c;int dir[4][2] = {{-1,0},{0,1},{1,0},{ 0,-1 }};int main(){while (~scanf("%d", &n)){int c = sqrt(n);while (n%c != 0) c--;r = n / c;for (int i = 1; i <= n; i++) scanf("%d", &a[i]);sort(a + 1, a + 1 + n,greater<int>());memset(ans, 0, sizeof ans);int x = 1, y = 1,p=0;for (int i = 1; i <= n; i++){ans[x][y] = a[i];if (i == n) break;while (1){int xx = x + dir[p][0];int yy = y + dir[p][1];if (ans[xx][yy] || xx<1 || xx>r || yy<1 || yy>c) { p++; p %= 4; continue; }x = xx, y = yy; break;}}for (int i = 1; i <= r; i++){printf("%d", ans[i][1]);for (int j = 2; j <= c; j++)printf(" %d", ans[i][j]);printf("\n");}}return 0;}
- PAT (Advanced Level) Practise 1105 Spiral Matrix (25)
- PAT (Advanced Level) Practise 1105 Spiral Matrix (25)
- 【PAT】【Advanced Level】1105. Spiral Matrix (25)
- PAT (Advanced Level) Practise 1105. Spiral Matrix (25) 蛇形填数
- 1105. Spiral Matrix (25)【模拟】——PAT (Advanced Level) Practise
- [PAT] (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise
- 1029. Median (25) @ PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1032. Sharing (25)
- PAT (Advanced Level) Practise 1003. Emergency (25)
- HDU
- 查看jvm内存使用情况
- 利用navicat创建存储过程、触发器和使用游标的简单实例
- LeetCode-447. Number of Boomerangs (Java)
- 【备忘】2017北京黑马17期JavaEE基础+就业班全套含解压密码
- PAT (Advanced Level) Practise 1105 Spiral Matrix (25)
- "沐事店员工具"App隐私政策
- 可用的redmine邮箱配置
- iOS 10新的通知机制中 展示图片、Gif、Audio和Video
- Redis 数据结构简介
- Spring集成Redis
- 【麦子学院】03.web前端开发之html5本地存储、网页留言板、video、audio、HTML5拖放
- Java及Python线程实现方式带实例
- 用windows系统下的DOS命令将腾讯视频客户端下载的qlv文件转换成MP4格式(图文详解)