-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathColourFill.cs
32 lines (27 loc) · 1.2 KB
/
ColourFill.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
namespace PracticeQuestionsSharp.Exercises.Dynamic_Programming
{
//Implement a colour fill method, like you would see in a paint application.
public static class ColourFill
{
//Fill each tile until you find a tile of a different colour.
public static int[,] FillColour(this int[,] canvas, int y, int x, int fillCol)
{
int baseCol = canvas[y, x];
int[,] memo = new int[canvas.GetLength(0), canvas.GetLength(1)];
memo.Initialize();
Fill(canvas, memo, y, x, fillCol, baseCol);
return canvas;
}
private static void Fill(int[,] canvas, int[,] memo, int y, int x, int fillCol, int baseCol)
{
if (memo[y, x] != 0) return;
if (canvas[y, x] != baseCol) return;
memo[y, x] = 1;
canvas[y, x] = fillCol;
if (y + 1 < canvas.GetLength(0)) Fill(canvas, memo, y + 1, x, fillCol, baseCol);
if (y - 1 >= 0) Fill(canvas, memo, y - 1, x, fillCol, baseCol);
if (x + 1 < canvas.GetLength(1)) Fill(canvas, memo, y, x + 1, fillCol, baseCol);
if (x - 1 >= 0) Fill(canvas, memo, y, x - 1, fillCol, baseCol);
}
}
}