螺旋矩阵算法问题

时间:2010-12-13 17:47:02

标签: c# java .net

嘿伙计们, 我的代码是填充螺旋矩阵。 当行=列时,它工作得很好 但是当它不同时它会在螺旋的第一次弯曲时出现错误! 我试过用断点调试它但是找不到任何错误! 关于我的代码的一些额外提示: bentCounter查找填充行或列的时间,如果是,则通过递增j来旋转螺旋。当j ++使用数组B和C的下一个元素时,因此方向改变了A数组的索引p,q! 当我们同时填充行然后是列时,子矩阵就这样留下了n--; M--; 当j = 3时,它应该无效以开始新的漩涡。 希望足够清楚!

static void Main(string[] args)
        {
            //n - quantity of rows, m - quantity of columns
            // p,q - references to current element of The Matrix A[][]
            // p=1, q=3 ----> A[0][3] - the element of crossing first row and fourth column

            int p = 0;
            int q = 0;
            int j = 0;
            int a = 0;
            int b = 0;
            int bentCounter = 0;

            a = int.Parse(Console.ReadLine());
            b = int.Parse(Console.ReadLine());

            int n = a;
            int m = b;
            int mn = m * n;

            int [,] A = new int [a,b];
            int[] B = new int[] { 0, 1, 0, -1 };
            int[] C = new int[] { 1, 0, -1, 0 };

            for (int i = 0; i < mn ; i++)
            {
                bentCounter++;
                if (bentCounter == n) {j++;}
                if (bentCounter == m + n - 1)
                {
                    if (j == 3) { j = -1; }
                    j++;
                    bentCounter = 0;
                    n--; m--;
                }

                A [p,q] = i;
                p += B[j];
                q += C[j];                              
            }

            for (int r = 0; r < A.GetLength(0); r++)
            {
                for (int c = 0; c < A.GetLength(1); c++)
                {
                    Console.Write(" " + A[r, c] + " ");
                }
                Console.WriteLine();
            }

10x感谢您的帮助 BR

4 个答案:

答案 0 :(得分:1)

你以错误的方式递增维度并超出界限,快速解决方案是将它们换成圆形,然后将int [,] A = new int [a,b]更改为

int [,] A = new int [b,a];

一切都很好;)

编辑:同时更改此行以填充输出会为您提供一个漂亮的方阵

Console.Write(" " + A[r, c].ToString().PadLeft(mn.ToString().Length, ' '));

答案 1 :(得分:1)

您还可以更改以下内容

if (bentCounter == n) { j++; }

if (bentCounter == m) { j++; }

它应该可以正常工作。原因是你的B和C前缀以这种方式组织。

答案 2 :(得分:1)

此外,下面的代码可能对那些想要创建具有给定大小的螺旋矩阵的人有用。

<a>

您也可以在Github Gist上看到此代码:Gist - C# Spiral Matrix

答案 3 :(得分:0)

试一试:它是一个100%动态螺旋矩阵供用户使用。

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <table>
            <tr>
                <td>Total Row Column :</td>
                <td>
                    <input type="text" runat="server" id="txtbox" style="width: 100px;" /></td>
            </tr>
            <tr>
                <td >Starting No :</td>
                <td>
                    <input type="text" runat="server" id="txtstartwith" style="width: 100px;" /></td>
            </tr>
            <tr>
                <td colspan="2" style="text-align:center;">
                    <asp:Button ID="Button1" runat="server" Text="Generate Spiral Matrix" OnClick="Button1_Click" /></td>
            </tr>
        </table>
        <div id="divsp" runat="server">
        </div>
    </form>
</body>
</html>

protected void Button1_Click(object sender, EventArgs e)
        {
            divsp.InnerHtml = "";
            int ignoreMe;
            bool successfullyParsed = int.TryParse(txtbox.Value, out ignoreMe);
            if (successfullyParsed)
            {
                successfullyParsed = int.TryParse(txtstartwith.Value, out ignoreMe);
                if (successfullyParsed)
                {
                    // ...
                }
                else
                {
                    txtstartwith.Value = "0";
                }
            }
            else
            {
                txtbox.Value = "0";
            }

            if (txtbox.Value != "" && txtbox.Value != "0" && Convert.ToInt32(txtbox.Value) > 0 && txtstartwith.Value != "")
            {
                int N = Convert.ToInt32(txtbox.Value);
                int row = N;
                int column = N;

                int[,] A = new int[N, N];
                int ele = N * N;
                ele = ele + (Convert.ToInt32(txtstartwith.Value) - 1);
                int no = Convert.ToInt32(txtstartwith.Value);

                int a = 0;
                int b = 0;

                int bo = 0;
                int le = 0;

                int loopbreak = 0;
                while (no <= ele)
                {
                    for (; b < N; b++)
                    {
                        A[a, b] = no;
                        if (no == ele)
                        {
                            loopbreak = 1;
                            break;
                        }
                        else
                        {
                            no++;
                        }

                    }
                    if (loopbreak == 1)
                    {
                        break;
                    }

                    if (b == N)
                    {
                        b = N - 1;
                        for (; a < b; a++)
                        {
                            int i = a + 1;

                            A[i, b] = no;
                            if (no == ele)
                            {
                                loopbreak = 1;
                                break;
                            }
                            else
                            {
                                no++;
                            }
                        }
                    }
                    if (loopbreak == 1)
                    {
                        break;
                    }
                    if (a == N - 1)
                    {
                        for (; b > le; b--)
                        {
                            int i = b - 1;
                            A[a, i] = no;
                            if (no == ele)
                            {
                                loopbreak = 1;
                                break;
                            }
                            else
                            {
                                no++;
                            }
                        }
                    }
                    if (loopbreak == 1)
                    {
                        break;
                    }
                    if (a == N - 1 && b == bo)
                    {
                        for (; a > le + 1; a--)
                        {
                            int i = a - 1;
                            A[i, b] = no;
                            if (no == ele)
                            {
                                loopbreak = 1;
                                break;
                            }
                            else
                            {
                                no++;
                            }
                        }
                        b = b + 1;
                    }
                    N = N - 1;
                    bo = bo + 1;
                    le = le + 1;
                }
                StringBuilder str = new StringBuilder();


                str.Append("<table>");
                for (int m = 0; m < row; m++)
                {
                    str.Append("<tr>");
                    for (int k = 0; k < row; k++)
                    {
                        str.Append("<td style='padding:5px;text-align:center;'>" + A[m, k].ToString() + "</td>");
                    }
                    str.Append("</tr>");
                }
                str.Append("</table>");
                divsp.InnerHtml = str.ToString().Trim();
            }
        }