当前位置:网站首页>1105 spiral matrix (25 points)
1105 spiral matrix (25 points)
2022-06-30 14:55:00 【Xue Dongjing】
1105 Spiral Matrix (25 branch )
The question
Give a group N Number , Put this N Fill in one number m*n In the spiral matrix . The spiral matrix is monotonically non increasing from the outer circle to the most central circle . The first number is in the upper left corner of the outermost circle . and m * n = N,m>=n And m And n It should be the group with the smallest difference among all the requirements m,n.
input
12
37 76 20 98 76 42 53 95 60 81 58 93
output
98 95 93
42 37 81
53 20 76
58 60 76
Ideas
A slightly troublesome simulation problem . It's easy to think , Find the one that meets the requirements first m,n, And sort this group of numbers from large to small , Then simulate the spiral matrix and fill in the numbers in turn .
look for m,n. It's easy to know ,n Must be less than or equal to N prescribing . from N Find the formula of , The first one is divisible N The number of is n,m be equal to N/n.
call sort Sort the sequence
The spiral matrix starts from the first to the right , And then down , Then to the left , Then go up , To the right …
It's easy to find patterns :
In the direction of : Right down Down to the left Left to top Up to right
When they encounter obstacles, they will change direction ( Out of matrix range , Hit the position where the number has been filled )
problem
The general idea of a simulation question is very simple , But there are many details . Maybe one condition is wrong, and the final result is far worse . Note the various boundaries and coordinates .
Code
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
int N, list[10007];
bool cmp(int a, int b){
return a>b;
}
int factor(int N){ // return n
int n;
n=sqrt(N);
for(int i=n; i>=1; i--){
if(N%i == 0){
return i;
}
}
}
void matr(){
int mat[107][107], vis[107][107], x=0, y=0, m, n;
int direc[2]={0,1};
memset(vis,0,sizeof(vis));
n=factor(N);
m=N/n;
mat[0][0]=list[0];
vis[0][0]=1;
for(int i=1; i<N; i++){
if(x+direc[1]<n&&x+direc[1]>=0&&y+direc[0]<m&&y+direc[0]>=0&&vis[y+direc[0]][x+direc[1]]==0){
y+=direc[0];
x+=direc[1];
mat[y][x]=list[i];
vis[y][x]=1;
// printf("mat[%d][%d] = %d\n",y,x,mat[y][x]);
}else{
if(direc[0]==0){
if(direc[1]==1){
direc[0]=-1;
direc[1]=0;
// printf("right to down\n");
}else{
direc[0]=1;
direc[1]=0;
// printf("left to up\n");
}
}else if(direc[0]==-1){
direc[0]=0;
direc[1]=-1;
// printf("down to left\n");
}else if(direc[0]==1){
direc[0]=0;
direc[1]=1;
// printf("up to right\n");
}
i-=1;
// printf("i= %d\n",i);
}
}
for(int i=0; i<m; i++){
if(i!=0){
printf("\n");
}
for(int j=0; j<n; j++){
if(j!=0){
printf(" ");
}
printf("%d",mat[i][j]);
}
}
}
int main()
{
// int list[10007];
scanf("%d",&N);
for(int i=0; i<N; i++){
scanf("%d",&list[i]);
}
sort(list, list+N,cmp);
// for(int i=0;i<N;i++){
// printf("%d ",list[i]);
// }
// printf("\n");
matr();
return 0;
}
边栏推荐
- @PathVariable
- Shift operator (detailed)
- 1130: find the first character that appears only once
- The difference between queue and stack
- Knowledge learned from the water resources institute project
- Matlab draws the image of the larger function value of the two functions (super simple)
- PS dynamic drawing
- Average and maximum values of MATLAB matrix
- Finding the median of two arrays by dichotomy
- Shangpinhui knowledge points of large e-commerce projects
猜你喜欢

Lihongyi machine learning 2020 homework summary

DiceCTF - knock-knock

After the MySQL service on the local computer is started and stopped, some services will automatically stop when they are not used by other services or programs

PS cutting height 1px, Y-axis tiling background image problem

day02

ES6 notes

Learn about data kinship JSON format design from sqlflow JSON format

Shangpinhui knowledge points of large e-commerce projects

JS to realize simple lottery function

CCF sequence segmentation (Full Score code + problem solving idea) 201509 -1
随机推荐
Add attributes to multimode
Double pointer palindrome string
Matlab calculates the factorial sum of the first n numbers (easy to understand)
[extensive reading of papers] analyzing connections between user attributes, images, and text
立式加工中心的数控加工对刀具使用基本要求
CCF Z-scan (full mark code + problem solving ideas) 201412-2
2021-05-12
Repair of incorrect deletion of win10 boot entry
The first dark spring cup dnuictf
Clear the route cache in Vue
Judgment of deep learning experiment results
分布式--OpenResty+lua+Redis
After the MySQL service on the local computer is started and stopped, some services will automatically stop when they are not used by other services or programs
Matlab construction operation example
DiceCTF - knock-knock
Examples of bubble sorting and matrix element screening in MATLAB
Maximum area of islands searched
1136: password translation
Three ways and differences of defining functions in JS
Programming exercises: special numbers (problem solving ideas + code implementation)