PAT----1050 螺旋矩阵(两种解法)

PAT(basic)–1050螺旋矩阵

题目描述

本题要求将给定的 N 个正整数按非递增的顺序,填入“螺旋矩阵”。所谓“螺旋矩阵”,是指从左上角第 1 个格子开始,按顺时针螺旋方向填充。要求矩阵的规模为 m 行 n 列,满足条件:m×n 等于 N;m≥n;且 m−n 取所有可能值中的最小值。

输入格式

输入在第 1 行中给出一个正整数 N,第 2 行给出 N 个待填充的正整数。所有数字不超过 10^4,相邻数字以空格分隔。

输出格式

输出螺旋矩阵。每行 n 个数字,共 m 行。相邻数字以 1 个空格分隔,行末不得有多余空格。

输入样例

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

输出样例

98 95 93
42 37 81
53 20 76
58 60 76

目录

== 本题的解法思路来源于leetcode 54.螺旋矩阵,用于学习和记录==
leetcode 54.螺旋矩阵 大家可以拿来一起强化学习
两种解法的核心的是模拟,解法一是将得到的元素一个一个放入正确的位置,解法二则是按层模拟,将最外层放入应该填入的元素,然后是次外层,以此类推,直到最内层。其实就是按照题意,然后我们用代码把这个过程模拟出来。
首先需要确定m和n的值

int m,n;  //m行,n列
for(n=sqrt(N);n>=1;n--) //N是输入的个数;
{
	if(N%n==0)
	{
		m=N/n;
		break;
	}
}

由于放入矩阵的元素是按非递减顺序放入,所以用一个vector容器来保存元素,之后再进行排序

bool cmp(int a,int b)
{
	return a>=b;
}

vector<int> a(N)
for(int i=0;i<N;i++)
{
	cin>>a[i];
}
sort(a.begin(),a.end(),cmp)

之后就是将我们得到的最后排好序的元素放入矩阵当中去了,先上解法一:
##思路
我们是按顺时钟的方向放入矩阵当中(先右后下再左再上),翻译成代码如下

int directions[4][2]={{0,1},{1,0},{0,-1},{-1,0}};

一共要放入N个元素,所以需要循环N次,循环里面我们还需要加一个判断,那就是什么时候需要换方向,两种情况,一是越界,二是再访问已经访问过的位置,所以这里再加一个book数组来判断下一个位置是否已经放入位置,核心代码如下:

vector<vector<int>> b(m,vector<int>>;
vector<vector<int>> book(m,vector<int>);
int row=0,col=0,directionIndex=0;
for(int i=0;i<N;i++)
{
	b[row][col]=a[i]
	book[row][col]=1;
	int nextRow=row+directions[directionIndex][0],nextCol=col+directions[directionIndex][1];
	if(nextRow<0 || nextRow>=m || nextCol<0 || nextCol>n || book[nextRow][nextCol]==1)
	{
		directionIndex=(directionIndex+1)%4;
	}
	row+=directions[directionIndex][0];
	col+=directions[directionIndex][1];
}

== 完整代码==

#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;

int directions[4][2]={{0,1},{1,0},{0,-1},{-1,0}};
bool cmp(int a,int b)
{
	return a>=b;
}

int main()
{
	int N,m,n;
	cin>>N;
	for(n=sqrt((double)N);n>=1;n--)
	{
		if(N%n==0)
		{
			m=N/n;
			break;
		}
	}
	vector<int> a(N);
	for(int i=0;i<N;i++)
	{
		cin>>a[i];
	}
	sort(a.begin(),a.end(),cmp);
	vector<vector<int>> b(m,vector<int>(n));
	
	vector<vector<int>> b(m,vector<int>>;
	vector<vector<int>> book(m,vector<int>);
	int row=0,col=0,directionIndex=0;
	for(int i=0;i<N;i++)
	{
		b[row][col]=a[i]
		book[row][col]=1;
		int nextRow=row+directions[directionIndex][0],nextCol=col+directions[directionIndex][1];
		if(nextRow<0 || nextRow>=m || nextCol<0 || nextCol>n || book[nextRow][nextCol]==1)
		{
			directionIndex=(directionIndex+1)%4;
		}
		row+=directions[directionIndex][0];
		col+=directions[directionIndex][1];
	}
	
	for(int j=0;j<m;j++)
	{
		
		for(int k=0;k<n;k++)
		{
			printf("%d ",b[j][k]);
		}
		printf("\n");
	}
		
	return 0;
}

解法二的思路和解法一基本是一样的,只不过从单个元素到一层元素,通过四个变量来进行每一层的遍历,left,right,top,bottom; //分别表示一层的左边,右边,上边,下边;核心代码如下:

int left=0,right=n-1,top=0,bottom=m-1,i=0;
	
	while(left<=right && top<=bottom)
	{
		for(int col=left;col<=right;col++)
		{
			b[top][col]=a[i++];
		}
		for(int row=top+1;row<=bottom;row++)
		{
			b[row][right]=a[i++];
		}
		if(left<right && top<bottom)
		{
			for(int col=right-1;col>=left+1;col--)
			{
				b[bottom][col]=a[i++];
			}
			for(int row=bottom;row>top;row--)
			{
				b[row][left]=a[i++];
			}
		}
		left++;
		right--;
		top++;
		bottom--;
	}

由于这里是通过一层一层的循环来进行装入元素,所以不需要判断是否会有二次访问的问题,但是要注意不能让left>right || top>bottom;
完整代码如下:

#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;

bool cmp(int a,int b)
{
	return a>=b;
}

int main()
{
	int N,m,n;
	cin>>N;
	for(n=sqrt((double)N);n>=1;n--)
	{
		if(N%n==0)
		{
			m=N/n;
			break;
		}
	}
	vector<int> a(N);
	for(int i=0;i<N;i++)
	{
		cin>>a[i];
	}
	sort(a.begin(),a.end(),cmp);
	vector<vector<int>> b(m,vector<int>(n));
	
	int left=0,right=n-1,top=0,bottom=m-1,i=0;
	
	while(left<=right && top<=bottom)
	{
		for(int col=left;col<=right;col++)
		{
			b[top][col]=a[i++];
		}
		for(int row=top+1;row<=bottom;row++)
		{
			b[row][right]=a[i++];
		}
		if(left<right && top<bottom)
		{
			for(int col=right-1;col>=left+1;col--)
			{
				b[bottom][col]=a[i++];
			}
			for(int row=bottom;row>top;row--)
			{
				b[row][left]=a[i++];
			}
		}
		left++;
		right--;
		top++;
		bottom--;
	}
	
	for(int j=0;j<m;j++)
	{
		
		for(int k=0;k<n;k++)
		{
			printf("%d ",b[j][k]);
		}
		printf("\n");
	}
		
	return 0;
}
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TIEZ@han

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值