1105 Spiral Matrix (25分)

本文详细解析了螺旋矩阵填充算法的实现过程,包括如何找到合适的矩阵尺寸和按顺时针方向填充元素的方法。通过具体实例和代码,展示了如何将一组整数按照非递增顺序填充到螺旋矩阵中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 10^​4.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

基本思路:使用二维数组按照一定顺序存储各个元素,最后以行为单位进行输出。

这道题有两个关键点
1、找到合适的m和n。
2、循环赋值。

先来看第一点,假设数据长度为N。要找到m-n最小且m>=n,那么可以利用一个从1开始的循环(用i计数),用N-i减去i,得到的结果取绝对值,选取绝对值最小的N-i 与 i 作为m 与 n 并根据大小关系分别赋值给m,n即可。

for(int i=1;i<=(int)sqrt(N);i++){
		if(N%i == 0){
			if(abs(N/i-i)<min){
				min = N/i-i;
				m = i>N/i?i:N/i;
				n = i>N/i?N/i:i;
			}
		}
	}

要注意,i的上界可以取到sqrt(N),避免重复计算。

说完第一点,再来看最关键的第二点:循环赋值问题。
刚开始接触这种题,看到我就头大,觉得很困难,无从下手。(类似的还有1109 Group Photo ),但是其实这种题是表面上难,写起来很简单,不相信的话我们继续往下看。
本题要求我们要顺时针赋值,那么我们就把这个赋值拆解开:拆为赋值几圈,每一圈分为上-右-下-左赋值,这样我们只用关注如何选取每个赋值循环的计数变量(如i,j)的上下界取值即可。赋值的圈数要根据元素数目来定,不需要人为的确认上界。

i为圈数(从0开始),j为每个赋值循环的计数变量。

while(cnt<N){
		for(int j=i;j<n-i && cnt<N;j++){
			G[i][j] = l[cnt++];
		}
		for(int j=i+1;j<m-i && cnt<N;j++){
			G[j][n-1-i] = l[cnt++];
		}
		for(int j=n-i-2;j>=i && cnt<N;j--){
			G[m-1-i][j] = l[cnt++];
		}
		for(int j=m-2-i;j>=i+1 && cnt<N;j--){
			G[j][i] = l[cnt++];
		}
		i++;
	}

我们以m=4,n=3来说明整个赋值过程:
第一圈(i=0)
对上方元素的赋值,是行为0,列从0到2。(行为i,列为【i,n-1-i】)
对右边元素的赋值,是行从1到3,列为2。(行为【i+1,m-1】列为n-1-i)
对下方元素的赋值,是行为3,列从1到0。(行为m-i,列为【n-i-2,i】)
对左边元素的赋值,是行从2到1,列为0。(行为【m-2-i,i+1】,列为i)

从这一圈分析,我们可以通过行列变化具体的数值来确定每一次赋值的上下界怎么取,由此我们便可以很容易的解决掉本题。

完整代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 10010;
int l[maxn];

int main(){
	int N;
	scanf("%d",&N);
	for(int i=0;i<N;i++){
		scanf("%d",&l[i]);
	}
	int min = 0x7FFFFFFF;
	int m,n;
	for(int i=1;i<=(int)sqrt(N);i++){
		if(N%i == 0){
			if(abs(N/i-i)<min){
				min = N/i-i;
				m = i>N/i?i:N/i;
				n = i>N/i?N/i:i;
			}
		}
	}
	int G[m][n];
	int i=0,cnt=0;
	sort(l,l+N,greater<int>());
	while(cnt<N){
		for(int j=i;j<n-i && cnt<N;j++){
			G[i][j] = l[cnt++];
		}
		for(int j=i+1;j<m-i && cnt<N;j++){
			G[j][n-1-i] = l[cnt++];
		}
		for(int j=n-i-2;j>=i && cnt<N;j--){
			G[m-1-i][j] = l[cnt++];
		}
		for(int j=m-2-i;j>=i+1 && cnt<N;j--){
			G[j][i] = l[cnt++];
		}
		i++;
	}
	for(int i=0;i<m;i++){
		for(int j=0;j<n;j++){
			printf("%d",G[i][j]);
			if(j<n-1)printf(" ");
		}
		printf("\n");
	}
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值