using
System;
using
System.Collections.Generic;
class
MatrixNode
{
public
int
val;
public
MatrixNode u, d, l, r;
public
MatrixNode(
int
val)
{
this
.val = val;
}
}
class
Program
{
static
MatrixNode buildOrthogonalList(
int
[,] matrix,
int
r,
int
c)
{
Dictionary<
int
, MatrixNode> mp =
new
Dictionary<
int
, MatrixNode>();
for
(
int
i = 0; i < r; i++)
{
for
(
int
j = 0; j < c; j++)
{
MatrixNode newNode =
new
MatrixNode(matrix[i, j]);
mp[(matrix[i, j])] = newNode;
if
(i != 0)
{
newNode.u = mp[(matrix[i - 1, j])];
mp[(matrix[i - 1, j])].d = newNode;
}
if
(j != 0)
{
newNode.l = mp[(matrix[i, j - 1])];
mp[(matrix[i, j - 1])].r = newNode;
}
}
}
return
mp[(matrix[0, 0])];
}
public
static
void
printOrthogonalList(MatrixNode head)
{
MatrixNode curRow = head;
MatrixNode cur =
null
;
while
(curRow !=
null
)
{
cur = curRow;
while
(cur !=
null
)
{
Console.Write(cur.val +
" "
);
cur = cur.r;
}
Console.WriteLine();
curRow = curRow.d;
}
}
static
void
Main(
string
[] args)
{
int
[,] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
MatrixNode list = buildOrthogonalList(matrix, 3, 3);
printOrthogonalList(list);
}
}