Advertisement
Guest User

FloodFill

a guest
Oct 10th, 2012
2,317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5.  
  6. namespace FloodFill
  7. {
  8.     internal class Program
  9.     {
  10.         private static void Main(string[] args)
  11.         {
  12.             var map = new Map(1024, 1024);
  13.  
  14.             var stopwatch = Stopwatch.StartNew();
  15.             {
  16.                 FloodFill(map, new Point(123, 123), fromValue: 0, toValue: 1);
  17.             }
  18.             stopwatch.Stop();
  19.  
  20.             Console.WriteLine(stopwatch.ElapsedMilliseconds);
  21.             Console.ReadLine();
  22.         }
  23.  
  24.         private static void FloodFill(Map map, Point start, int fromValue, int toValue)
  25.         {
  26.             if (map[start.X, start.Y] == fromValue == false)
  27.             {
  28.                 return;
  29.             }
  30.  
  31.             map[start.X, start.Y] = toValue;
  32.  
  33.             var open = new Queue<Point>();
  34.             open.Enqueue(start);
  35.  
  36.             while (open.Count > 0)
  37.             {
  38.                 var current = open.Dequeue();
  39.                 var x = current.X;
  40.                 var y = current.Y;
  41.  
  42.                 if (x > 0)
  43.                 {
  44.                     if (map[x - 1, y] == fromValue)
  45.                     {
  46.                         map[x - 1, y] = toValue;
  47.                         open.Enqueue(new Point(x - 1, y));
  48.                     }
  49.                 }
  50.                 if (x < map.Width - 1)
  51.                 {
  52.                     if (map[x + 1, y] == fromValue)
  53.                     {
  54.                         map[x + 1, y] = toValue;
  55.                         open.Enqueue(new Point(x + 1, y));
  56.                     }
  57.                 }
  58.                 if (y > 0)
  59.                 {
  60.                     if (map[x, y - 1] == fromValue)
  61.                     {
  62.                         map[x, y - 1] = toValue;
  63.                         open.Enqueue(new Point(x, y - 1));
  64.                     }
  65.                 }
  66.                 if (y < map.Height - 1)
  67.                 {
  68.                     if (map[x, y + 1] == fromValue)
  69.                     {
  70.                         map[x, y + 1] = toValue;
  71.                         open.Enqueue(new Point(x, y + 1));
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.     }
  77.  
  78.     internal class Map
  79.     {
  80.         private readonly int[] data;
  81.         private readonly int height;
  82.         private readonly int width;
  83.  
  84.         public Map(int height, int width)
  85.         {
  86.             this.height = height;
  87.             this.width = width;
  88.  
  89.             data = new int[height * width];
  90.         }
  91.  
  92.         public int Height
  93.         {
  94.             get { return height; }
  95.         }
  96.  
  97.         public int Width
  98.         {
  99.             get { return width; }
  100.         }
  101.  
  102.         public int this[int x, int y]
  103.         {
  104.             get { return data[x + y * width]; }
  105.             set { data[x + y * width] = value; }
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement