Created
May 31, 2015 16:03
-
-
Save VegaFromLyra/81fa8868345758869a10 to your computer and use it in GitHub Desktop.
PaintFill
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| namespace PaintFill | |
| { | |
| public class Program | |
| { | |
| public static void Main(string[] args) | |
| { | |
| Point [,] canvas = { | |
| { new Point(0, 0, Color.Red), new Point(0, 1, Color.Red) }, | |
| { new Point(1, 0, Color.Red), new Point(1, 1, Color.Red) }, | |
| }; | |
| Console.WriteLine("Original canvas:"); | |
| display(canvas); | |
| paintFill(canvas, 0, 1, Color.Green); | |
| Console.WriteLine("Repainted canvas:"); | |
| display(canvas); | |
| } | |
| static void display(Point[,] canvas) { | |
| for (int i = 0; i < canvas.GetLength(0); i++) { | |
| for (int j = 0; j < canvas.GetLength(1); j++) { | |
| if (canvas[i, j].Color == Color.Red) { | |
| Console.Write("R "); | |
| } else { | |
| Console.Write("G "); | |
| } | |
| } | |
| Console.WriteLine(); | |
| } | |
| } | |
| // Note: Only works if the passed in point does not have the new color | |
| static bool paintFill(Point[,] canvas, int row, int column, Color newColor) { | |
| if (row < 0 || | |
| row >= canvas.GetLength(0) || | |
| column < 0 || | |
| column >= canvas.GetLength(1)) { | |
| return false; | |
| } | |
| if (canvas[row, column].Color != newColor) { | |
| canvas[row, column].Color = newColor; | |
| paintFill(canvas, row - 1, column, newColor); | |
| paintFill(canvas, row + 1, column, newColor); | |
| paintFill(canvas, row, column - 1, newColor); | |
| paintFill(canvas, row, column + 1, newColor); | |
| } | |
| return true; | |
| } | |
| } | |
| enum Color { | |
| NoColor, | |
| Green, | |
| Red | |
| }; | |
| class Point { | |
| public Point(int row, int column, Color color) { | |
| Row = row; | |
| Column = column; | |
| Color = color; | |
| } | |
| public int Row {get; private set;} | |
| public int Column {get; private set;} | |
| public Color Color {get; set;} | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment