PAT (Basic Level) 1050 matrix helix (analog)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_45458915/article/details/102721274

Topic links: Click here

Title effect: given number N, the helical configuration in descending order after the matrix n rows and columns required to meet the requirements m n * m == N and n> m and (mn) as small as possible

Topic Analysis: After the sort is simply fill in the number of the serpentine, while online are using a set of four while writing, I like to use the wording of the maze to write, this question has a very crucial point is that the array size , because the topics to ensure that all the numbers are within the 1e4, but if we want to open 1e4 * 1e4 matrix, is certainly no less open, so there will be a direct pay up test sample T out, then how can we do it? There is a critical message that n> m, and (mn) as small as possible, so we can analyze, when N maximum take time to 1e4, will surely n and m is divided into sqrt (1e4) = 100 is most appropriate, but when N is a prime number greater than 100, in which case only divided into (N,. 1), which are two extreme cases, and here we can see, only the maximum value of 100 m, and n it may reach a maximum value 1e4, so we started the group can directly open maze [1e4] [1e2], rather than a maze [1e4] [1e4], and really learned, because the details of the half-tone hours code, always T out, it turned out to be much of a reason to open an array

I have to say that this topic is a special data card this point, see if I can think of

Code:

#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream> 
#include<deque>
#include<unordered_map>
#define Pi acos(-1.0)
using namespace std;

typedef long long LL;

const int inf=0x3f3f3f3f;

const int N=1e4+100;

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

int n,m;

int a[N];

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

void getnm(int x)
{
	int mark;
	for(int i=1;i<=sqrt(x);i++)
		if(x%i==0)
			mark=i;
	m=mark;
	n=x/mark;
}

int maze[N][100];

bool vis[N][100];

bool check(int x,int y)
{
	if(x<0||y<0||x>=n||y>=m)
		return false;
	if(vis[x][y])
		return false;
	return true;
}

int main()
{
//  freopen("input.txt","r",stdin);
    int N;
    scanf("%d",&N);
    for(int i=1;i<=N;i++)
	    scanf("%d",a+i);
    sort(a+1,a+1+N,cmp);
    getnm(N);
    int cnt=1;
    maze[0][0]=a[cnt++];
    vis[0][0]=true;
    int x=0,y=0;
    int pos=0;
    while(cnt<=N)
    {
    	int xx=x+b[pos][0];
    	int yy=y+b[pos][1];
    	while(check(xx,yy))
    	{
    		maze[xx][yy]=a[cnt++];
    		vis[xx][yy]=true;
    		x=xx;
    		y=yy;
    		xx=x+b[pos][0];
    		yy=y+b[pos][1];
		}
    	pos=(pos+1)%4;
	}
	for(int i=0;i<n;i++)
	{
		cout<<maze[i][0];
		for(int j=1;j<m;j++)
			cout<<' '<<maze[i][j];
		cout<<endl;
	}
    
    
    
    
    
    

    return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_45458915/article/details/102721274