【PTA/乙级】【1050】 螺旋矩阵 (25 分) 暴力+方向向量

螺旋一直不是很擅长没想到一遍过了。手动螺旋(bushi

r(行数)等于最接近srt(n)且大于等于sqrt(n)的因子,想到了题一半就完成了。

c(列数)等于n/行数。

最后一个测试点可以假设n是一个大素数。

#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
int b[10005][105];


int main()
{
	int n, r, c;
	cin >> n;
	vector<int>a(n + 1);
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}

	//非严格递减
	sort(a.begin() + 1, a.end(), [](const int& x, const int& y) {
		return x > y;
		});

	// 寻找最接近sqrt(n)且大于等于sqrt(n)的因子
	for (int i = n; i >= sqrt(n); i--) {
		if (n % i == 0) {
			r = i;
		}
	}
	c = n / r;

	int d = 0, x = 1, y = 1, cnt = 1;
	b[x][y] = a[cnt];

	while (cnt < n)//手动螺旋矩阵
	{
		while (y < c && !b[x][y + 1]) {
			b[x][++y] = a[++cnt];
		}

		while (x < r && !b[x + 1][y]) {
			b[++x][y] = a[++cnt];
		}

		while (y > 1 && !b[x][y - 1]) {
			b[x][--y] = a[++cnt];
		}

		while (x > 1 && !b[x - 1][y]) {
			b[--x][y] = a[++cnt];
		}
	}

	//输出结果
	for (int i = 1; i <= r; i++)
	{
		for (int j = 1; j <= c; j++)
		{
			cout << b[i][j];
			if (j == c) cout << endl;
			else cout << ' ';
		}
	}
	return 0;
}

方向向量(这个好,强烈推荐)

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

//方向向量
int mx[] = { 0,1,0,-1 };
int my[] = { 1,0,-1,0 };

int b[10005][105]; 
int r, c;

bool inmap(int x, int y)//判断是否越界
{
	return x >= 1 && x <= r && y >= 1 && y <= c;
}

int main()
{
	int n;
	cin >> n;
	vector<int>a(n + 1);
	for (int i = 1; i <= n; i++){
		cin >> a[i];
	}
	sort(a.begin() + 1, a.end(), [](const int& x, const int& y) {
		return x > y;
		});
	for (int i = n; i >= sqrt(n); i--){
		if (n % i == 0) {
				r = i;
		}
	}
	c = n / r;
	int x = 1, y = 1, cnt = 1, k = 0;
	b[x][y] = a[cnt];

	while (cnt < n)
	{
		x += mx[k]; y += my[k];//朝该方向移动一次

		while (!b[x][y] && inmap(x,y) ) {
			b[x][y] = a[++cnt];
			x += mx[k], y += my[k];
		}
		x -= mx[k]; y -= my[k];//退出时多移动了一次,还原!!!

		k = (k + 1) % 4;
	}

	//输出
	for (int i = 1; i <= r; i++)
	{
		for (int j = 1; j <= c; j++)
		{
			cout << b[i][j];
			if (j == c) cout << endl;
			else cout<<' ';
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Sophon、

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

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

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

打赏作者

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

抵扣说明:

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

余额充值