C#中的螺旋算法

时间:2009-07-25 20:45:10

标签: c# algorithm

如何填充数组:

1 2 3 4 5 6 7  8 
20 21 22 23 24 9
19 30 31 32 25 10
18 29 28 27 26 11
17 16 15 14 13 12

螺旋 C# 感谢

6 个答案:

答案 0 :(得分:10)

从元素(0,0)(左上角)开始遍历数组,向右移动(增加列索引)。保持一个运行的计数器,每次填充元素时都会递增,以及您尚未填充的行和列的上限和下限。对于M列乘N列矩阵,行边界应为0和(M-1),并且列的边界为0和(N-1)。向右走,直到你达到上限列,递减你的上行界限,向下直到你达到你的上行界限,递减你的上行界限,向左走直到你达到你的下列界限,增加你的下列界限,向上直到你达到你的下行界限,增加你的下限,并重复,直到你的上行和下行或列边界相等(或直到你的运行计数是M * N)。

答案 1 :(得分:2)

我亲自制作了一个节目。检查一下。

using System;
using System.Collections.Generic;
using System.Text;

namespace SpiralMatrix
{
    class Program
    {
        static void Main(string[] args)
        {
            int m = 0, n = 0, start = 0, step = 0;
            bool errorOcured = false;
            Console.WriteLine("====Spiral Matrix====\n");
            try
            {
                Console.WriteLine("Enter size of the matrix:");
                Console.Write("Row (m)? ");
                m = Convert.ToInt32(Console.ReadLine());
                Console.Write("Column (n)? ");
                n = Convert.ToInt32(Console.ReadLine());

                Console.Write("Enter the starting number: ");
                start = Convert.ToInt32(Console.ReadLine());
                Console.Write("Enter step: ");
                step = Convert.ToInt32(Console.ReadLine());
                if (m < 0 || n < 0 || start < 0 || step < 0) throw new FormatException();
            }
            catch (FormatException e)
            {
                Console.WriteLine("Wrong input. [Details: {0}]", e.Message);
                Console.WriteLine("Program will now exit...");
                errorOcured = true;
            }

            if (!errorOcured)
            {
                int[,] mat = new int[m, n];
                mat = initMatrix(m, n, start, step);

                Console.WriteLine("\nIntial matrix generated is:");
                displayMatrix(mat, m, n);

                Console.WriteLine("\nSpiral Matrix generated is:");
                mat = calculateSpider(mat, m, n);
                displayMatrix(mat, m, n);
            }
            Console.Write("\nPress enter to exit...");
            Console.Read();
        }
        private static int[,] initMatrix(int m, int n, int start, int step)
        {
            int[,] ret = new int[m, n];
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    ret[i, j] = start;
                    start += step;
                }
            }
            return ret;
        }
        private static void displayMatrix(int[,] mat, int m, int n)
        {
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.Write("\t{0}", mat[i, j]);
                }
                Console.WriteLine();
            }
        }
        private static int[,] calculateSpider(int[,] mat, int m, int n)
        {
            int[,] intMat;
            if (m <= 2 || n <= 2)
            {

                if (m == 2 && n == 2)
                {
                    int[,] t = new int[m, n];
                    t[0, 0] = mat[0, 0];
                    t[0, 1] = mat[0, 1];
                    t[1, 0] = mat[1, 1];
                    t[1, 1] = mat[1, 0];
                    return t;
                }
                else if (m == 2)
                {
                    int[,] t = new int[m, n];
                    for (int i = 0; i < n; i++)
                    {
                        t[0, i] = mat[0, i];
                        t[1, n - 1 - i] = mat[1, i];
                    }
                    return t;
                }
                else if (n == 2)
                {
                    int[,] t = new int[m, n];
                    int[] stMat = new int[m * n];
                    int c = 0;
                    for (int i = 0; i < m; i++)
                    {
                        for (int j = 0; j < n; j++)
                        {
                            stMat[c] = mat[i, j];
                            c++;
                        }
                    }
                    c = 0;
                    for (int i = 0; i < n; i++)
                    {
                        t[0, i] = stMat[c];
                        c++;
                    }
                    for (int i = 1; i < m; i++)
                    {
                        t[i, 1] = stMat[c];
                        c++;
                    }
                    if(m>1) t[m - 1, 0] = stMat[c];
                    c++;
                    for (int i = m - 2; i >= 1; i--)
                    {
                        t[i, 0] = stMat[c];
                        c++;
                    }
                    return t;
                }
                else return mat;
            }
            intMat = new int[m - 2, n - 2];
            int[,] internalMatrix = new int[m - 2, n - 2]; //internal matrix
            for (int i = 0; i < ((m - 2) * (n - 2)); i++)
            {
                internalMatrix[(m - 2) - 1 - i / (n - 2), (n - 2) - 1 - i % (n - 2)] = mat[m - 1 - (i / n), n - 1 - (i % n)];
            }
            intMat = calculateSpider(internalMatrix, m - 2, n - 2);

            int[,] retMat = new int[m, n]; //return matrix
            //copy some characters to a single dimentional array
            int[] tempMat = new int[(m * n) - ((m - 2) * (n - 2))];
            for (int i = 0; i < (m * n) - ((m - 2) * (n - 2)); i++)
            {
                tempMat[i] = mat[i / n, i % n];
            }
            int count = 0;
            //copy fist row
            for (int i = 0; i < n; i++)
            {
                retMat[0, i] = tempMat[count];
                count++;
            }
            //copy last column
            for (int i = 1; i < m; i++)
            {
                retMat[i, n - 1] = tempMat[count];
                count++;
            }
            //copy last row
            for (int i = n - 2; i >= 0; i--)
            {
                retMat[m - 1, i] = tempMat[count];
                count++;
            }
            //copy first column
            for (int i = m - 2; i >= 1; i--)
            {
                retMat[i, 0] = tempMat[count];
                count++;
            }
            //copy others
            for (int i = 1; i < m - 1; i++)
            {
                for (int j = 1; j < n - 1; j++)
                {
                    retMat[i, j] = intMat[i - 1, j - 1];
                }
            }
            return retMat;
        }
    }
}

答案 2 :(得分:1)

Web上有几种解决方案,包括StackOverflow和其他地方:

答案 3 :(得分:0)

好吧,我不会给你代码。你不会从我身上学到任何东西,但是我会给你一个提示。

如果您要使用此螺旋图案填充N x M矩形,请注意其中的N-1 x M-1矩形也是螺旋图案。同样,内部的N-2 x M-2矩形也是一个螺旋图案,依此类推,直到你有一个1x1的矩形。

因此很可能是一种递归解决方案。

答案 4 :(得分:0)

您可以这样做:

    class Spiral
{
    int[,] matrix;
    int m_size;
    int currentCount;

    static void Main(string[] args)
    {
        Spiral s = new Spiral(2);
        s.DrawSpiral();
        Console.ReadLine();
    }

    public Spiral(int size)
    {
        this.m_size = size;                 
        matrix = new int[size, size];
        currentCount = 1;
    }

    public void DrawSpiral()
    {
        //x,y   x, y+size-1
        //x+1, y+size-1     x+size-1, y+size-1
        //x+size-1, y+size-2    x+size-1, y
        //x+size-2, y   x+1, y
        int x = 0, y = 0, size = m_size;

        while (size > 0)
        {
            for (int i = y; i <= y + size - 1; i++)
            {
                matrix[x, i] = currentCount++;
            }

            for (int j = x + 1; j <= x + size - 1; j++)
            {
                matrix[j, y + size - 1] = currentCount++;
            }

            for (int i = y + size - 2; i >= y; i--)
            {
                matrix[x + size - 1, i] = currentCount++;
            }

            for (int i = x + size - 2; i >= x + 1; i--)
            {
                matrix[i, y] = currentCount++;
            }

            x = x + 1;
            y = y + 1;
            size = size - 2;
        }
        PrintMatrix();
    }

    private void PrintMatrix()
    {
        for (int i = 0; i < m_size; i++)
        {
            for (int j = 0; j < m_size; j++)
            {
                Console.Write(matrix[i, j]);
                Console.Write("   ");
            }
            Console.WriteLine();
        }
    }
}

答案 5 :(得分:0)

只需在2循环中输入x和y(对于你的,-2,-2到2,2)

while(y <= 2)<br>
{<br>
  while(x <= 2)<br>
  {<br>
    // PRINT RESULTS OF (25-spiral_get_value(x,y))<br>
    x += 1;<br>
  }<br>
  x = -2;<br>
  y += 1;<br>
}

逻辑就在这里。您将不得不操纵它以与C#一起使用。下面的算法立即计算(x,y)处的螺旋数,从from [0,0] == 1开始,顺时针旋转,where [0,-1] == 2.

在将x / y值放入算法之前简单地否定它们,或者将x替换为y并取消其中一个或两个,以改变方向(顺时针/逆时针),输出是否被翻转(水平,垂直或两者)等等。

// spiral_get_value(x,y);<br>
sx = argument0;<br>
sy = argument1;<br>
a = max(sqrt(sqr(sx)),sqrt(sqr(sy)));<br>
c = -b;<br>
d = (b*2)+1;<br>
us = (sy==c and sx !=c);<br>
rs = (sx==b and sy !=c);<br>
bs = (sy==b and sx !=b);<br>
ls = (sx==c and sy !=b);<br>
ra = rs*((b)*2);<br>
ba = bs*((b)*4);<br>
la = ls*((b)*6);<br>
ax = (us*sx)+(bs*-sx);<br>
ay = (rs*sy)+(ls*-sy);<br>
add = ra+ba+la+ax+ay;<br>
value = add+sqr(d-2)+b;<br>
return(value);