Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,4 +1,7 @@
|
|||
Produce a zig-zag array. A zig-zag array is a square arrangement of the first <tt>N<sup>2</sup></tt> integers, where the numbers increase sequentially as you zig-zag along the anti-diagonals of the array. For a graphical representation, see [[wp:Image:JPEG_ZigZag.svg|JPG zigzag]] (JPG uses such arrays to encode images).
|
||||
Produce a zig-zag array.
|
||||
A zig-zag array is a square arrangement of the first <tt>N<sup>2</sup></tt> integers, where the numbers increase sequentially as you zig-zag along the anti-diagonals of the array. <br>
|
||||
For a graphical representation, see [[wp:Image:JPEG_ZigZag.svg|JPG zigzag]]
|
||||
(JPG uses such arrays to encode images).
|
||||
|
||||
For example, given <tt>5</tt>, produce this array:
|
||||
<pre>
|
||||
|
|
@ -8,3 +11,5 @@ For example, given <tt>5</tt>, produce this array:
|
|||
9 11 17 20 22
|
||||
10 18 19 23 24
|
||||
</pre>
|
||||
|
||||
;See also [[Spiral matrix]]
|
||||
|
|
|
|||
23
Task/Zig-zag-matrix/C/zig-zag-matrix.c
Normal file
23
Task/Zig-zag-matrix/C/zig-zag-matrix.c
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int c, char **v)
|
||||
{
|
||||
int i, j, m, n, *s;
|
||||
|
||||
/* default size: 5 */
|
||||
if (c < 2 || ((m = atoi(v[1]))) <= 0) m = 5;
|
||||
|
||||
/* alloc array*/
|
||||
s = malloc(sizeof(int) * m * m);
|
||||
|
||||
for (i = n = 0; i < m * 2; i++)
|
||||
for (j = (i < m) ? 0 : i-m+1; j <= i && j < m; j++)
|
||||
s[(i&1)? j*(m-1)+i : (i-j)*m+j ] = n++;
|
||||
|
||||
for (i = 0; i < m * m; putchar((++i % m) ? ' ':'\n'))
|
||||
printf("%3d", s[i]);
|
||||
|
||||
/* free(s) */
|
||||
return 0;
|
||||
}
|
||||
41
Task/Zig-zag-matrix/CoffeeScript/zig-zag-matrix.coffee
Normal file
41
Task/Zig-zag-matrix/CoffeeScript/zig-zag-matrix.coffee
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
# Calculate a zig-zag pattern of numbers like so:
|
||||
# 0 1 5
|
||||
# 2 4 6
|
||||
# 3 7 8
|
||||
#
|
||||
# There are many interesting ways to solve this; we
|
||||
# try for an algebraic approach, calculating triangle
|
||||
# areas, so that me minimize space requirements.
|
||||
|
||||
zig_zag_value = (x, y, n) ->
|
||||
|
||||
upper_triangle_zig_zag = (x, y) ->
|
||||
# calculate the area of the triangle from the prior
|
||||
# diagonals
|
||||
diag = x + y
|
||||
triangle_area = diag * (diag+1) / 2
|
||||
# then add the offset along the diagonal
|
||||
if diag % 2 == 0
|
||||
triangle_area + y
|
||||
else
|
||||
triangle_area + x
|
||||
|
||||
if x + y < n
|
||||
upper_triangle_zig_zag x, y
|
||||
else
|
||||
# For the bottom right part of the matrix, we essentially
|
||||
# use reflection to count backward.
|
||||
bottom_right_cell = n * n - 1
|
||||
n -= 1
|
||||
v = upper_triangle_zig_zag(n-x, n-y)
|
||||
bottom_right_cell - v
|
||||
|
||||
zig_zag_matrix = (n) ->
|
||||
row = (i) -> (zig_zag_value i, j, n for j in [0...n])
|
||||
(row i for i in [0...n])
|
||||
|
||||
do ->
|
||||
for n in [4..6]
|
||||
console.log "---- n=#{n}"
|
||||
console.log zig_zag_matrix(n)
|
||||
console.log "\n"
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
int[][] zigZag(in int n) pure nothrow {
|
||||
static void move(in int n, ref int i, ref int j) pure nothrow {
|
||||
int[][] zigZag(in int n) pure nothrow @safe {
|
||||
static void move(in int n, ref int i, ref int j)
|
||||
pure nothrow @safe @nogc {
|
||||
if (j < n - 1) {
|
||||
if (i > 0) i--;
|
||||
j++;
|
||||
|
|
@ -18,5 +19,6 @@ int[][] zigZag(in int n) pure nothrow {
|
|||
|
||||
void main() {
|
||||
import std.stdio;
|
||||
writefln("%(%(%2d %)\n%)", zigZag(5));
|
||||
|
||||
writefln("%(%(%2d %)\n%)", 5.zigZag);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,14 @@
|
|||
import std.stdio, std.algorithm, std.typecons, std.range, std.array;
|
||||
import std.stdio, std.algorithm, std.range, std.array;
|
||||
|
||||
int[][] zigZag(int n) {
|
||||
alias P2 = Tuple!(int,"x", int,"y");
|
||||
auto L = iota(n ^^ 2).map!(i => P2(i % n, i / n)).array;
|
||||
|
||||
L.sort!q{ (a.x + a.y == b.x + b.y) ?
|
||||
((a.x + a.y) % 2 ? a.y < b.y : a.x < b.x) :
|
||||
(a.x + a.y) < (b.x + b.y) };
|
||||
int[][] zigZag(in int n) pure nothrow {
|
||||
static struct P2 { int x, y; }
|
||||
const L = iota(n ^^ 2).map!(i => P2(i % n, i / n)).array
|
||||
.sort!q{ (a.x + a.y == b.x + b.y) ?
|
||||
((a.x + a.y) % 2 ? a.y < b.y : a.x < b.x) :
|
||||
(a.x + a.y) < (b.x + b.y) }.release;
|
||||
|
||||
auto result = new typeof(return)(n, n);
|
||||
foreach (i, p; L)
|
||||
foreach (immutable i, immutable p; L)
|
||||
result[p.y][p.x] = i;
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
66
Task/Zig-zag-matrix/MATLAB/zig-zag-matrix.m
Normal file
66
Task/Zig-zag-matrix/MATLAB/zig-zag-matrix.m
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
function matrix = zigZag(n)
|
||||
|
||||
%This is very unintiutive. This algorithm parameterizes the
|
||||
%zig-zagging movement along the matrix indicies. The easiest way to see
|
||||
%what this algorithm does is to go through line-by-line and write out
|
||||
%what the algorithm does on a peace of paper.
|
||||
|
||||
matrix = zeros(n);
|
||||
counter = 1;
|
||||
flipCol = true;
|
||||
flipRow = false;
|
||||
|
||||
%This for loop does the top-diagonal of the matrix
|
||||
for i = (2:n)
|
||||
row = (1:i);
|
||||
column = (1:i);
|
||||
|
||||
%Causes the zig-zagging. Without these conditionals,
|
||||
%you would end up with a diagonal matrix.
|
||||
%To see what happens, comment these conditionals out.
|
||||
if flipCol
|
||||
column = fliplr(column);
|
||||
flipRow = true;
|
||||
flipCol = false;
|
||||
elseif flipRow
|
||||
row = fliplr(row);
|
||||
flipRow = false;
|
||||
flipCol = true;
|
||||
end
|
||||
|
||||
%Selects a diagonal of the zig-zag matrix and places the
|
||||
%correct integer value in each index along that diagonal
|
||||
for j = (1:numel(row))
|
||||
matrix(row(j),column(j)) = counter;
|
||||
counter = counter + 1;
|
||||
end
|
||||
end
|
||||
|
||||
%This for loop does the bottom-diagonal of the matrix
|
||||
for i = (2:n)
|
||||
row = (i:n);
|
||||
column = (i:n);
|
||||
|
||||
%Causes the zig-zagging. Without these conditionals,
|
||||
%you would end up with a diagonal matrix.
|
||||
%To see what happens comment these conditionals out.
|
||||
if flipCol
|
||||
column = fliplr(column);
|
||||
flipRow = true;
|
||||
flipCol = false;
|
||||
elseif flipRow
|
||||
row = fliplr(row);
|
||||
flipRow = false;
|
||||
flipCol = true;
|
||||
end
|
||||
|
||||
%Selects a diagonal of the zig-zag matrix and places the
|
||||
%correct integer value in each index along that diagonal
|
||||
for j = (1:numel(row))
|
||||
matrix(row(j),column(j)) = counter;
|
||||
counter = counter + 1;
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
|
@ -69,8 +69,6 @@ compute_next(_NL, NC, Lig, Col, up, Lig1, Col, down) :-
|
|||
Lig1 is Lig + 1.
|
||||
|
||||
|
||||
|
||||
|
||||
print_line(L) :-
|
||||
maplist(print_val, L),
|
||||
nl.
|
||||
|
|
|
|||
|
|
@ -1,7 +1,3 @@
|
|||
#ZigZag
|
||||
#
|
||||
# Nigel Galloway: June 7th., 2012,
|
||||
#
|
||||
COLS = 9
|
||||
def CX(x, ran):
|
||||
while True:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue