-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiralMatrix.cs
59 lines (49 loc) · 1003 Bytes
/
SpiralMatrix.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
namespace ConsoleApp82
{
internal class Program
{
private static void Main()
{
int[,] arr = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10},
{11, 12, 13, 14, 15}, {16, 17, 18, 19, 20}};
var R = 4;
var C = 5;
SpiralMatrix(R, C, arr);
}
private static void SpiralMatrix(int r, int c, int[,] arr)
{
int k = 0, l=0;
while (k < r && l < c)
{
int i;
for ( i = l; i < c; ++i)
{
Console.WriteLine(arr[k,i] +" ");
}
k++;
for ( i = k; i < r; ++i)
{
Console.WriteLine(arr[i,c-1] +" ");
}
c--;
if (k < r)
{
for (i = c - 1; i >= l; --i)
{
Console.WriteLine(arr[r-1,i] +" ");
}
r--;
}
if (l < c)
{
for ( i =r-1; i>=k;--i)
{
Console.WriteLine(arr[i,l]+ " ");
}
l++;
}
}
}
}
}