螺旋矩阵填充最简单的方法--1105 Spiral Matrix (25 分)

矩阵 螺旋填充可以提前规定好上下左右四个方向,然后根据所在的位置是否越界或者是否已经被 填充过相应地改变方向。比如 dx=[1,0,-1,0], dy=[0,1,0,-1],设置初试方向 dir=0,每次移动的 时候只要当前位置加上对应的 dx 和 dy,要改变方向的时候就 dir=(dir+1)%4 就可以了,是否 填充满可以直接用填充数量判断。

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

AC代码:

#include<cstdio>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#pragma warning(disable:4996)
using namespace std;
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
const int maxn = 100 + 10;
const int INF = 0x7fffffff;
bool cmp(int &x, int &y) {
	return x > y;
}
int main(){
	int n;
	scanf("%d", &n);
	int row = n, col = 1;
	vector<int> ve(n);
	for (int i = 0; i < n; i++)
		scanf("%d", &ve[i]);
	sort(ve.begin(), ve.end(), cmp);
	int min_sub = INF;
	for (int i = 1; i <= sqrt(n); i++) {
		if (n%i == 0) {
			if (abs(i - n / i) < min_sub) {
				row = i;
				col = n / row;
			}
		}
	}
	if (col > row)swap(col, row);
	//printf("%d %d", row, col);
	//for (int i = 0; i < n; i++)
	//	printf("%d ", ve[i]);
	//printf("\n");
	int ans[maxn][maxn] = { 0 }, book[maxn][maxn] = { 0 };
	int cnt = 0, dir = 0;
	int x = 0, y = 0;
	int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
	while (cnt < n) {
		ans[y][x] = ve[cnt];
		book[y][x] = 1;
		//printf("x:%d y:%d\n", x, y);
		//for (int i = 0; i < row; i++)
		//	for (int j = 0; j < col; j++)
		//		printf("%d%c", ans[i][j], j == col - 1 ? '\n' : ' ');
		//printf("\n\n");
		if (x + dx[dir] < 0 || x + dx[dir] == col || 
			y + dy[dir] < 0 || y + dy[dir] == row || 
			book[y + dy[dir]][x + dx[dir]] == 1)
			dir = (dir + 1) % 4;
		x = x + dx[dir];
		y = y + dy[dir];
		cnt++;
	}
	for (int i = 0; i < row; i++)
		for(int j = 0;j < col; j++)
			printf("%d%c", ans[i][j], j == col - 1 ? '\n' : ' ');
	return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本人经过几年的努力,终于掌握了C#的编程,与同学一起开发了这套软件。与各位网友共勉。 1.5版正式推出!主要修正了(以下都是重大问题所以应及时更新,对给您造成的不便,本公司深表歉意): 1、重装软件后提示30天试用期已过的BUG; 2、机器码和注册码的0和字母O不好区的问题。 3、一个注册表错误,会使软件重启后当成试用期结束。 “世间能有几回运,彩票游戏存玄机” ,也许您会有疑问:彩票开奖号码不是完全随机的吗?那我还算什么,随便写几个数字买算了!!其实您错了,世界上并没有绝对理想的随机数,就算是用电脑也只能产生接近随机的数:在彩票开奖产生号码时,诸如摇奖机的物理特性、每个球的重量和光滑度的差异、空气的流动性等等都会使开奖结果产生一定的偏态,在中短期内有一定的规律可寻。 那么怎样才能找出偏态,利用偏态提高中奖的概率呢?用手工显然不行,您需要一款称手软件!但是现在彩票软件多得让人眼花缭乱,哪个才是适合您的呢?得彩易彩票旋转矩阵选号杀号王双色球专用版的问世,解决了您的烦恼,您只要下载使用了,软件就会主动成为您的好帮手,让您中奖不再是梦想! 本公司双色球项目开发小组根据组里多名长年研究彩票双色球的专家的成果,经过反复的概率验证,精心设计了算法,其中大部属于本公司原创独有。程序运行时随着期数的增加,出号趋势会愈发明显,算法也会愈发准确,一般开奖期数在最近50-150期时效果最为明显。 友情提示:彩票软件可以帮您提高中奖的概率,但并不能保证您100%中大奖,本软件也不例外!如果有所谓专家向您推荐号码并保证能中大奖的,请您一定要当心受骗! 1、本软件基于微软.net框架进行开发,技术先进。 2、市面上的一般彩票软件所提供的旋转矩阵只有廖廖几个方案(如保6中5,保5中4),本软件则提供了几十种方案,应有尽有。 3、选号杀号功能的强大和全面是其它一般彩票软件所没有的。 4、过滤功能全面,完全可以满足需要。 5、开奖数据可以更新,导入和导出,极大的方便彩民朋友。 6、学习容易,帮助文档详尽,上手极快。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值