Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- namespace FloodFill
- {
- internal class Program
- {
- private static void Main(string[] args)
- {
- var map = new Map(1024, 1024);
- var stopwatch = Stopwatch.StartNew();
- {
- FloodFill(map, new Point(123, 123), fromValue: 0, toValue: 1);
- }
- stopwatch.Stop();
- Console.WriteLine(stopwatch.ElapsedMilliseconds);
- Console.ReadLine();
- }
- private static void FloodFill(Map map, Point start, int fromValue, int toValue)
- {
- if (map[start.X, start.Y] == fromValue == false)
- {
- return;
- }
- map[start.X, start.Y] = toValue;
- var open = new Queue<Point>();
- open.Enqueue(start);
- while (open.Count > 0)
- {
- var current = open.Dequeue();
- var x = current.X;
- var y = current.Y;
- if (x > 0)
- {
- if (map[x - 1, y] == fromValue)
- {
- map[x - 1, y] = toValue;
- open.Enqueue(new Point(x - 1, y));
- }
- }
- if (x < map.Width - 1)
- {
- if (map[x + 1, y] == fromValue)
- {
- map[x + 1, y] = toValue;
- open.Enqueue(new Point(x + 1, y));
- }
- }
- if (y > 0)
- {
- if (map[x, y - 1] == fromValue)
- {
- map[x, y - 1] = toValue;
- open.Enqueue(new Point(x, y - 1));
- }
- }
- if (y < map.Height - 1)
- {
- if (map[x, y + 1] == fromValue)
- {
- map[x, y + 1] = toValue;
- open.Enqueue(new Point(x, y + 1));
- }
- }
- }
- }
- }
- internal class Map
- {
- private readonly int[] data;
- private readonly int height;
- private readonly int width;
- public Map(int height, int width)
- {
- this.height = height;
- this.width = width;
- data = new int[height * width];
- }
- public int Height
- {
- get { return height; }
- }
- public int Width
- {
- get { return width; }
- }
- public int this[int x, int y]
- {
- get { return data[x + y * width]; }
- set { data[x + y * width] = value; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement