Hi, 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
Hi, 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
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).
There are several solutions to that in the Web, both on StackOverflow and elsewhere:
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.
So there is most likely a recursive solution.
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();
}
}
}
Hi... 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;
}
}
}