Advertisement
Guest User

FloodFill

a guest
Oct 11th, 2012
974
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4.  
  5. namespace FloodFill
  6. {
  7.     internal class Program
  8.     {
  9.         private const int MAP_SIZE = 1024; // only powers of two are supported
  10.  
  11.         private static void Main()
  12.         {
  13.             var map = new byte[MAP_SIZE * MAP_SIZE];
  14.  
  15.             var stopwatch = Stopwatch.StartNew();
  16.             {
  17.                 FloodFill(map, startX: 123, startY: 123, fromValue: 0, toValue: 1);
  18.             }
  19.             stopwatch.Stop();
  20.  
  21.             Console.WriteLine(stopwatch.ElapsedMilliseconds);
  22.             Console.ReadLine();
  23.         }
  24.  
  25.         private static void FloodFill(byte[] map, int startX, int startY, byte fromValue, byte toValue)
  26.         {
  27.             int shift = (int) Math.Round(Math.Log(map.Length, 4)); // if the array's length is (2^x * 2^x), then size = x
  28.             int startIndex = startX + (startY << shift);
  29.  
  30.             if (map[startIndex] != fromValue)
  31.             {
  32.                 return;
  33.             }
  34.  
  35.             int size = 1 << shift;
  36.             int sizeMinusOne = size - 1;
  37.             int xMask = size - 1;
  38.  
  39.             map[startIndex] = toValue;
  40.  
  41.             var queue = new Queue<int>();
  42.             queue.Enqueue(startIndex);
  43.  
  44.             while (queue.Count > 0)
  45.             {
  46.                 int index = queue.Dequeue();
  47.                 int x = index & xMask;
  48.                 int y = index >> shift;
  49.  
  50.                 if (x > 0)
  51.                 {
  52.                     if (map[index - 1] == fromValue)
  53.                     {
  54.                         map[index - 1] = toValue;
  55.                         queue.Enqueue(index - 1);
  56.                     }
  57.                 }
  58.                 if (x < sizeMinusOne)
  59.                 {
  60.                     if (map[index + 1] == fromValue)
  61.                     {
  62.                         map[index + 1] = toValue;
  63.                         queue.Enqueue(index + 1);
  64.                     }
  65.                 }
  66.                 if (y > 0)
  67.                 {
  68.                     if (map[index - size] == fromValue)
  69.                     {
  70.                         map[index - size] = toValue;
  71.                         queue.Enqueue(index - size);
  72.                     }
  73.                 }
  74.                 if (y < sizeMinusOne)
  75.                 {
  76.                     if (map[index + size] == fromValue)
  77.                     {
  78.                         map[index + size] = toValue;
  79.                         queue.Enqueue(index + size);
  80.                     }
  81.                 }
  82.             }
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement