C#中的螺旋算法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1183013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spiral Algorithm in C#
提问by
How can I fill an array like so:
我怎样才能像这样填充数组:
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
Spiral C# Thanks
螺旋 C# 谢谢
采纳答案by Matt J
Traverse the array starting from element (0,0) (top-left), and heading right (incrementing your column index). Keep a running counter that increments each time you fill an element, as well as upper and lower bounds on the rows and columns you have yet to fill. For an M-row by N-column matrix, your row bounds should be 0 and (M-1), and your column bounds 0 and (N-1). Go right until you hit your upper column bound, decrement your upper column bound, go down until you hit your upper row bound, decrement your upper row bound, go left until you hit your lower column bound, increment your lower column bound, go up until you hit your lower row bound, increment your lower bound, and repeat until your upper and low row or column bounds are equal (or until your running count is M*N).
从元素 (0,0)(左上角)开始遍历数组,向右(增加列索引)。保留一个每次填充元素时递增的运行计数器,以及尚未填充的行和列的上限和下限。对于 M 行乘 N 列矩阵,行边界应为 0 和 (M-1),列边界应为 0 和 (N-1)。向右走直到您达到上列边界,减少您的上列边界,向下直到您到达您的上行边界,减少您的上行边界,向左走直到您到达您的下列边界,增加您的下列边界,向上直到你达到你的下限,增加你的下限,然后重复直到你的上限和下限行或列的界限相等(或者直到你的运行计数是 M*N)。
回答by Martin v. L?wis
There are several solutions to that in the Web, both on StackOverflow and elsewhere:
在 Web 上有几种解决方案,包括 StackOverflow 和其他地方:
回答by IRBMe
Well, I won't give you the code. You won't learn anything from me figuring it out for you, but I'll give you a hint.
好吧,我不会给你代码。你不会从我身上学到任何东西,但我会给你一个提示。
If you have an N x M rectangle that you want to fill in with this spiral pattern, notice that the N-1 x M-1 rectangle inside of that is also a spiral pattern. Similarly, the N-2 x M-2 rectangle inside that is also a spiral pattern, and so on until you have a 1x1 rectangle.
如果您想用此螺旋图案填充 N x M 矩形,请注意其内部的 N-1 x M-1 矩形也是螺旋图案。同样,里面的 N-2 x M-2 矩形也是一个螺旋图案,依此类推,直到你有一个 1x1 的矩形。
So there is most likely a recursive solution.
所以很可能有一个递归解决方案。
回答by jkals
You can do something like this:
你可以这样做:
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();
}
}
}
回答by chanchal1987
I personally made a program. Check it.
我个人做了一个程序。核实。
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;
}
}
}
回答by Daniel Price
simply input x and y (for yours, -2, -2 to 2,2) in 2 while loops
只需在 2 个 while 循环中输入 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>
}
The logic is all here. You will have to manipulate it for use with C#. The algorithm below instantly calculates the spiral number at (x,y), starting from [0,0] == 1
and spiraling out clockwise, where [0,-1] == 2.
逻辑都在这里。您将不得不操纵它以与 C# 一起使用。下面的算法立即计算 (x,y) 处的螺旋数,from [0,0] == 1
顺时针开始和螺旋出,where [0,-1] == 2.
Simply negate the x / y values before placing them in the algorithm, or swap x for y and negate either or both, to change the direction (clockwise / anticlockwise), whether the output is flipped (horizontally, vertically or both), etc.
只需在将 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);