(2D) - . 3D :
class Program
{
class ArrayPoint { public int x; public int y;}
private static byte[,] startArray =
{
{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,5,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0},{0,0,5,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0}
};
private static int rows = startArray.GetLength(0);
private static int cols = startArray.GetLength(1);
static void Main(string[] args)
{
Spread(startArray);
}
static void Spread(byte[,] array)
{
var points = GetStartPoints(array);
foreach (var point in points.ToList())
SpreadPoint(array, point.x, point.y);
Display(array);
}
static void SpreadPoint(byte[,] array, int x, int y)
{
for (var i = x-1; i < x+2; i++)
for (var j = y-1; j < y+2; j++)
if ( (i==x || j==y) && !(i==x && j==y) && (i >= 0 && i < rows && j >= 0 && j < cols)
&& array[i, j] + 1 < array[x, y])
{
array[i, j] = (byte)(array[x, y] - 1);
SpreadPoint(array, i, j);
}
}
static void Display(byte[,] array)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
Console.Write("{0} ",array[i,j]);
Console.WriteLine();
}
Console.WriteLine();
}
static IEnumerable<ArrayPoint> GetStartPoints(byte[,] array)
{
for (var i = 0; i < rows; i++)
for (var j = 0; j < cols; j++)
if (array[i, j] != 0)
yield return new ArrayPoint {x = i, y = j};
}
}
:
0 0 0 1 2 1 0 0 0 0
0 0 1 2 3 2 1 0 0 0
0 1 2 3 4 3 2 1 0 0
1 2 3 4 5 4 3 2 1 0
0 1 2 3 4 3 2 1 0 0
0 1 2 2 3 2 1 0 0 0
1 2 3 2 2 1 0 0 0 0
2 3 4 3 2 1 0 0 0 0
3 4 5 4 3 2 1 0 0 0
2 3 4 3 2 1 0 0 0 0