PAT (Advanced Level) 1105. Spiral Matrix (25)

简单模拟。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
#include<string>
#include<algorithm>
using namespace std;

const int maxn=10000+10;
int N,a[maxn];
int ans[maxn];
int m,n;
int dir[4][2];

void f()
{
    dir[0][0]=0;dir[0][1]=1;
    dir[1][0]=1;dir[1][1]=0;
    dir[2][0]=0;dir[2][1]=-1;
    dir[3][0]=-1;dir[3][1]=0;
}

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

int main()
{
    scanf("%d",&N);
    f();
    for(int i=1;i<=N;i++) scanf("%d",&a[i]);
    sort(a+1,a+1+N,cmp);

    for(int i=sqrt(1.0*N);i>=1;i--)
        if(N%i==0){n=i,m=N/i;break;}

    memset(ans,-1,sizeof ans);
    for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)
            ans[(i-1)*n+j]=0;

    int nowDir=0;
    int nowX=1,nowY=1;
    for(int i=1;i<=N;i++)
    {
        ans[(nowX-1)*n+nowY]=a[i];
        int NewX=nowX+dir[nowDir][0];
        int NewY=nowY+dir[nowDir][1];
        if(ans[(NewX-1)*n+NewY]!=0||NewX<=0||NewX>=m+1||NewY<=0||NewY>=n+1) nowDir=(nowDir+1)%4;
        nowX=nowX+dir[nowDir][0];
        nowY=nowY+dir[nowDir][1];
    }
    int num=1;
   // for(int i=1;i<=m*n;i++) printf("%d ",ans[i]);

    for(int i=1;i<=m;i++)
        for(int j=1;j<=n;j++)
        {
            printf("%d",ans[num++]);
            if(j<n) printf(" ");
            else printf("\n");
        }
    return 0;
}
时间: 2024-12-16 09:34:30

PAT (Advanced Level) 1105. Spiral Matrix (25)的相关文章

1105. Spiral Matrix (25)【模拟】——PAT (Advanced Level) Practise

题目信息 1105. Spiral Matrix (25) 时间限制150 ms 内存限制65536 kB 代码长度限制16000 B 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 co

PAT 甲级 1105 Spiral Matrix (25分)(螺旋矩阵,简单模拟)

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

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 c

PAT (Advanced Level) 1083. List Grades (25)

简单排序. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std

PAT (Advanced Level) 1021. Deepest Root (25)

先并查集判断连通性,然后暴力每个点作为根节点判即可. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; struct Edge { int a,b; }e[20000]; int n,sz

PAT (Advanced Level) 1020. Tree Traversals (25)

递归建树,然后BFS一下 #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; const int maxn=40; int a[maxn],b[maxn]; int n,tot; struc

PAT (Advanced Level) 1114. Family Property (25)

简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; struct X { int id; int Father,Mother; int k;

PAT (Advanced Level) 1085. Perfect Sequence (25)

可以用双指针(尺取法),也可以枚举起点,二分终点. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; int n; long long k; long l

PAT (Advanced Level) 1071. Speech Patterns (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std;