Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
25
Task/Zig-zag-matrix/C-sharp/zig-zag-matrix.cs
Normal file
25
Task/Zig-zag-matrix/C-sharp/zig-zag-matrix.cs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
public static int[,] ZigZag(int n)
|
||||
{
|
||||
int[,] result = new int[n, n];
|
||||
int i = 0, j = 0;
|
||||
int d = -1; // -1 for top-right move, +1 for bottom-left move
|
||||
int start = 0, end = n * n - 1;
|
||||
do
|
||||
{
|
||||
result[i, j] = start++;
|
||||
result[n - i - 1, n - j - 1] = end--;
|
||||
|
||||
i += d; j -= d;
|
||||
if (i < 0)
|
||||
{
|
||||
i++; d = -d; // top reached, reverse
|
||||
}
|
||||
else if (j < 0)
|
||||
{
|
||||
j++; d = -d; // left reached, reverse
|
||||
}
|
||||
} while (start < end);
|
||||
if (start == end)
|
||||
result[i, j] = start;
|
||||
return result;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue