【算法题目】 产生螺旋队列

// shuangshuang.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<stdio.h>
#include<malloc.h>
#include<iomanip>
//#include"myheader.h"
using namespace std;



///生成zigzag数

int luoxuan(int m, int n)  //任意输入坐标,输出队列相应的位置
{
    int N = abs(m)>abs(n)? abs(m):abs(n);
    int *startIndex =new int [N+1];
    int **startPoint =new int *[N+1];

    for(int i=0;i<N+1;i++)
    {
        startPoint[i] = new int [2];
    }

    if(startIndex==0 || startPoint==0 ) //判断是否申请成功
    {
        cout<<"error";
        exit(1);
    }
    
;

    startIndex[0]=0;
    startPoint[0][0]=0;
    startPoint[0][1]=0;
    for(int i=1;i<=N;i++) //记录每一层的起始点的数字大小
    {
        startIndex[i]=(2*i-1)*(2*i-1)+1;
    }

    for(int i=1;i<=N;i++)//记录每一层的起始点的位置
    {    
        startPoint[i][0]=i;
        startPoint[i][1]=-(i-1);
    }

    if( m==N && n==-(N))// 分类讨论在四条边上的情况
        return (2*N+1)*(2*N+1);
    else if(m == N)
        return startIndex[N]+n-startPoint[N][1];
    else if(n == N)
        return startIndex[N]+N-startPoint[N][1] + N-m;
    else if (m == -N)
        return startIndex[N]+N-startPoint[N][1] + 2*N+N-n;
    else
        return startIndex[N]+N-startPoint[N][1] + 2*N+2*N+m+N;

}


int main()
{
    

    int a,b;
    int N=5;//队列的大小



    for(int i=-N;i<N;i++)
    {
        for(int j=-N;j<N;j++)
        {
            cout<<setw(3)<<luoxuan(i,j)<<"  ";

        }
        cout<<endl;
    }

}
原文地址:https://www.cnblogs.com/Dzhouqi/p/3628325.html