Beispiel #1
0
        public override void Run()
        {
            var bm = new BinaryMatrix(2, 2);
            var m  = new int[][]
            {
                new int[] { 1, 3 },
                new int[] { 2, 3 },
            };

            bm.Intialize(m);
            var output = LeftMostColumnWithOne(bm);
        }
        static Binary GenerateVerificationBits(BinaryMatrix H, Binary message)
        {
            Binary verification = new Binary(new bool[H.RowAmount]);

            for (int i = 0; i < H.RowAmount; i++)
            {
                Binary row             = H.GetRow(i);
                Binary addiction       = row & message;
                bool   verificationBit = addiction.CountOnes() % 2 == 1 ? true : false;
                verification[i] = verificationBit;
            }
            return(verification);
        }
        private static int FindFaultyBit(BinaryMatrix H, Binary s)
        {
            for (int i = 0; i < H.ColumnAmount; i++)
            {
                Binary column = H.GetColumn(i);
                Binary check  = s ^ column;
                if (check.Any(b => b))
                {
                    continue;
                }
                return(i);
            }

            throw new WarningException("Faulty bit not found!");
        }
Beispiel #4
0
    public void Solve()
    {
        int N = Reader.Int();
        var S = Reader.StringArray(N);
        var M = new BinaryMatrix(N, N);

        for (int r = 0; r < N; r++)
        {
            for (int c = 0; c < N; c++)
            {
                M[r, c] = S[r][c] - '0';
            }
        }
        Console.WriteLine(M.GaussJordan(N) < N ? "Even" : "Odd");
    }
    public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
    {
        var dims = binaryMatrix.Dimensions();
        var rows = dims[0];
        var cols = dims[1];
        var idx  = int.MaxValue;

        for (var row = 0; row < rows; ++row)
        {
            var idxRow = FindIdx(binaryMatrix, row, cols);
            if (idxRow != -1)
            {
                idx = Math.Min(idx, idxRow);
            }
        }

        return(idx == int.MaxValue ? -1 : idx);
    }
Beispiel #6
0
        public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
        {
            var dimensions = binaryMatrix.Dimensions();

            var row = 0;
            var col = dimensions[1] - 1;

            while (row < dimensions[0])
            {
                while (col >= 0 && binaryMatrix.Get(row, col) == 1)
                {
                    col--;
                }
                row++;
            }

            return(col + 1 == dimensions[1] ? -1 : col + 1);
        }
    public void Find(BinaryMatrix bm, int r, int c)
    {
        int v = bm.Get(r, c);

        if (v == 1)
        {
            MinIndex = c;
        }

        if (v == 0 && r < NumRows - 1)
        {
            Find(bm, r + 1, c);
        }

        if (v == 1 && c > 0)
        {
            Find(bm, r, c - 1);
        }
    }
        public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
        {
            var dimensions = binaryMatrix.Dimensions();
            int?currentMin = null;

            for (int i = 0; i < dimensions[0]; i++)//rows
            {
                if (binaryMatrix.Get(i, dimensions[1] - 1) == 0)
                {
                    continue;
                }

                int start = 0, end = currentMin.HasValue ? currentMin.Value : dimensions[1] - 1, mid;
                while (start <= end)
                {
                    if (binaryMatrix.Get(i, end) != 1 || start < 0 || end >= dimensions[1])
                    {
                        break;
                    }

                    mid = start + (end - start) / 2;

                    if (binaryMatrix.Get(i, start) == 1)
                    {
                        currentMin = start;
                        break;
                    }

                    if (binaryMatrix.Get(i, mid) == 1)
                    {
                        currentMin = mid;
                        end        = mid - 1;
                    }
                    else
                    {
                        currentMin = end;
                        start      = mid + 1;
                    }
                }
            }

            return(currentMin.HasValue ? currentMin.Value : -1);
        }
Beispiel #9
0
        public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
        {
            var dimensions = binaryMatrix.Dimensions();
            var result     = -1;
            var x          = 0;
            var y          = dimensions[1] - 1;

            while (x < dimensions[0] && y >= 0)
            {
                if (binaryMatrix.Get(x, y) == 0)
                {
                    x++;
                }
                else
                {
                    result = y--;
                }
            }
            return(result);
        }
Beispiel #10
0
        public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
        {
            IList <int> dimensions = binaryMatrix.Dimensions();
            int         rows       = dimensions[0];
            int         cols       = dimensions[1];

            int leftMost = cols;

            for (int i = 0; i < rows; i++)
            {
                if (binaryMatrix.Get(i, cols - 1) == 0)
                {
                    continue;
                }
                int minCol = FindLeftMostColumn(binaryMatrix, i, cols);
                if (minCol < leftMost)
                {
                    leftMost = minCol;
                }
            }
            return(leftMost == cols ? -1 : leftMost);
        }
Beispiel #11
0
        public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
        {
            var size   = binaryMatrix.Dimensions();
            var result = -1;
            var row    = size[0] - 1;
            var col    = size[1] - 1;

            while (row >= 0 && col >= 0)
            {
                if (binaryMatrix.Get(row, col) == 0)
                {
                    row--;
                }
                else if (binaryMatrix.Get(row, col) == 1)
                {
                    result = col;
                    col--;
                }
            }

            return(result);
        }
    private static int FindIdx(BinaryMatrix binaryMatrix, int row, int cols)
    {
        var lo = 0;
        var hi = cols - 1;

        while (lo <= hi)
        {
            var m   = lo + (hi - lo) / 2;
            var val = binaryMatrix.Get(row, m);

            if (val == 0)
            {
                lo = m + 1;
            }
            else
            {
                hi = m - 1;
            }
        }

        return(lo < cols ? lo : -1);
    }
        public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
        {
            var dimentions = binaryMatrix.Dimensions();
            var rowIndex   = 0;
            var colIndex   = dimentions[1] - 1;

            while (colIndex >= 0 && rowIndex < dimentions[0])
            {
                var matrixValue = binaryMatrix.Get(rowIndex, colIndex);

                if (matrixValue == 1)
                {
                    colIndex--;
                }
                else
                {
                    rowIndex++;
                }
            }

            return(colIndex == dimentions[1] - 1 ? -1 : colIndex + 1);
        }
Beispiel #14
0
    private int GetNonZeroLeftIndex(int i, int cols, BinaryMatrix matrix)
    {
        var left         = 0;
        var right        = cols;
        var firstNonZero = -1;

        while (left <= right)
        {
            var mid = left + (right - left) / 2;

            if (matrix.Get(i, mid) == 0)
            {
                left = mid + 1;
            }
            else
            {
                right        = mid - 1;
                firstNonZero = mid;
            }
        }

        return(firstNonZero);
    }
Beispiel #15
0
        public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
        {
            var dia          = binaryMatrix.Dimensions();
            int r            = dia[0] - 1;
            int c            = dia[1] - 1;
            int lastClouindx = c;

            for (int i = 0; i < r; i++)
            {
                for (int j = lastClouindx; j > 0; j--)
                {
                    if (binaryMatrix.Get(i, j) == 1)
                    {
                        lastClouindx = j;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return(lastClouindx == c?-1: lastClouindx + 1);
        }
Beispiel #16
0
        private int FindLeftMostColumn(BinaryMatrix binaryMatrix, int i, int cols)
        {
            int start = 0;
            int end   = cols - 2;
            int min   = cols - 1;

            while (start <= end)
            {
                int mid = (start + end) / 2;
                if (binaryMatrix.Get(i, mid) == 1)
                {
                    if (mid < min)
                    {
                        min = mid;
                    }
                    end = mid - 1;
                }
                else
                {
                    start = mid + 1;
                }
            }
            return(min);
        }
Beispiel #17
0
        public void LeftMostColumnWithOneTests()
        {
            var matrix = new[, ]
            {
                { 0, 0 },
                { 0, 0 }
            };
            var binaryMatrix = new BinaryMatrix(matrix);

            Assert.Equal(-1, LeftMostColumnWithOne(binaryMatrix));

            matrix = new[, ]
            {
                { 0, 0 },
                { 1, 1 }
            };
            binaryMatrix = new BinaryMatrix(matrix);
            Assert.Equal(0, LeftMostColumnWithOne(binaryMatrix));

            matrix = new[, ]
            {
                { 0, 0 },
                { 0, 1 }
            };
            binaryMatrix = new BinaryMatrix(matrix);
            Assert.Equal(1, LeftMostColumnWithOne(binaryMatrix));

            matrix = new[, ]
            {
                { 0, 0, 0, 1 },
                { 0, 0, 1, 1 },
                { 0, 1, 1, 1 }
            };
            binaryMatrix = new BinaryMatrix(matrix);
            Assert.Equal(1, LeftMostColumnWithOne(binaryMatrix));
        }
Beispiel #18
0
    public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
    {
        IList <int> dim    = binaryMatrix.Dimensions();
        int         row    = dim[0];
        int         column = dim[1];

        int result = -1;
        int r      = 0;
        int c      = column - 1;

        while (r < row && c >= 0)
        {
            if (binaryMatrix.Get(r, c) == 1)
            {
                result = c;
                c--;
            }
            else
            {
                r++;
            }
        }
        return(result);
    }
        static BinaryMatrix GenerateHMatrix(int rowsAmount, int columnsAmount)
        {
            BinaryMatrix H = new BinaryMatrix(rowsAmount, columnsAmount);

            int n = 0;

            for (int i = 1; i <= Math.Pow(2, rowsAmount); i++)
            {
                Binary binary = new Binary(i, H.RowAmount);
                if (binary.CountOnes() >= 2)
                {
                    for (int y = 0; y < rowsAmount; y++)
                    {
                        H.Set(y, n, binary[y]);
                    }
                    n++;
                }
                if (n >= H.ColumnAmount)
                {
                    break;
                }
            }
            return(H);
        }
        static BinaryMatrix GenerateHWithIdentity(BinaryMatrix H)
        {
            BinaryMatrix HWithIdentity = new BinaryMatrix(H.RowAmount, H.ColumnAmount + H.RowAmount);

            for (int y = 0; y < H.RowAmount; y++)
            {
                for (int x = 0; x < H.ColumnAmount; x++)
                {
                    HWithIdentity.Set(y, x, H.Get(y, x));
                }
            }

            for (int y = 0; y < H.RowAmount; y++)
            {
                int n = 0;
                for (int x = H.ColumnAmount; x < H.ColumnAmount + H.RowAmount; x++)
                {
                    HWithIdentity.Set(y, x, y == n);

                    n++;
                }
            }
            return(HWithIdentity);
        }
Beispiel #21
0
        public int LeftMostColumnWithOne(BinaryMatrix binaryMatrix)
        {
            /////Use the API interface #1
            List <int> x = (List <int>)binaryMatrix.Dimensions();

            //start from top & right
            int currentrow = x[0] - 1;
            int currentcol = x[1] - 1;

            int leftmost = -1;

            while (currentcol >= 0 && currentrow >= 0)
            {
                ////Use the API Interface #2
                int temp = binaryMatrix.Get(currentrow, currentcol);

                if (temp == 1) //Move left while it is ONE
                {
                    leftmost = currentcol;
                    currentcol--;
                }
                else //Move down if run into Zero
                {
                    currentrow--;
                }
            }

            if (currentcol == -1)//run out of the matrix from left side
            {
                return(0);
            }
            else    //run out of the matrix from bottom................
            {
                return(leftmost);
            }
        }
        public void Hamming()
        {
            Binary message;

            message = new Binary(new[] { true, false, true, false });
            int columnsAmount = message.Count();
            int rowsAmount    = (int)Math.Ceiling(Math.Log(columnsAmount, 2) + 1);
            int parity_bit    = 0;

            Binary       bin_parity_bit = new Binary(new[] { false });
            BinaryMatrix H            = GenerateHMatrix(rowsAmount, columnsAmount);
            Binary       verification = GenerateVerificationBits(H, message);
            Binary       help_frame   = Binary.Concatenate(message, verification);

            if (help_frame.CountOnes() % 2 == 0)
            {
                bin_parity_bit = new Binary(new[] { false });
                parity_bit     = 0;
            }
            if (help_frame.CountOnes() % 2 != 0)
            {
                bin_parity_bit = new Binary(new[] { true });
                parity_bit     = 1;
            }

            Binary frame = Binary.Concatenate(help_frame, bin_parity_bit);

            //Console.WriteLine("Message = {0}", message);
            //Console.WriteLine("H matrix:");
            //Console.Write(H);
            //Console.WriteLine("Verification = {0}", verification);
            //Console.WriteLine("Output Frame = {0}", frame);
            //Console.WriteLine();


            //Console.WriteLine("*** Receiving ***");
            //Console.WriteLine("Corrupt message? [y/n]");
            bool?corruptMessage = null;

            //do
            //{
            //    switch (Console.ReadKey().Key)
            //    {
            //        case ConsoleKey.Y:
            //            corruptMessage = true;
            //            break;
            //        case ConsoleKey.N:
            //            corruptMessage = false;
            //            break;
            //    }
            //  Console.WriteLine();
            // }
            while (!corruptMessage.HasValue)
            {
                ;
            }

            int    frame_lenght  = frame.Length;
            Binary receivedFrame = new Binary(frame.ToArray());

            if (corruptMessage.Value)
            {
                int badBit  = random.Next(0, receivedFrame.Length - 1);
                int badBit2 = random.Next(0, receivedFrame.Length - 1);

                receivedFrame[badBit]  = !receivedFrame[badBit2];
                receivedFrame[badBit2] = !receivedFrame[badBit2];
            }
            Binary recivedFrameWithParitBit = new Binary(receivedFrame.ToArray().Take(frame_lenght));
            Binary receivedParityBit        = new Binary(frame.ToArray().Skip(frame_lenght - 1));

            receivedFrame = new Binary(receivedFrame.ToArray().Take(frame_lenght - 1));
            Binary receivedMessage      = new Binary(receivedFrame.Take(columnsAmount));
            Binary receivedVerification = new Binary(receivedFrame.Skip(columnsAmount));
            int    generatedParityBit   = -1;

            H = GenerateHMatrix(rowsAmount, columnsAmount);
            Binary receivedMessageVerification = GenerateVerificationBits(H, receivedMessage);
            Binary s = receivedVerification ^ receivedMessageVerification;

            if (recivedFrameWithParitBit.CountOnes() % 2 == 0)
            {
                generatedParityBit = 0;
            }
            if (recivedFrameWithParitBit.CountOnes() % 2 != 0)
            {
                generatedParityBit = 1;
            }

            //Console.WriteLine("received frame = {0}", Binary.Concatenate(receivedFrame, receivedParityBit));
            //Console.WriteLine("H matrix:");
            //Console.Write(H);
            //Console.WriteLine("received message verification: {0}", receivedMessageVerification);
            //Console.WriteLine("s value: {0}", s);

            if (s.CountOnes() > 0)
            {
                if (generatedParityBit == 1)
                {
                    try
                    {
                        BinaryMatrix HWithIdentity     = GenerateHWithIdentity(H);
                        int          faultyBitPosition = FindFaultyBit(HWithIdentity, s);

                        Binary correctedFrame = new Binary(receivedFrame.ToArray());
                        correctedFrame[faultyBitPosition] = !correctedFrame[faultyBitPosition];

                        //Console.WriteLine("H matrix with identity:");
                        //Console.Write(HWithIdentity);
                        //Console.WriteLine("Fault bit possition founded: {0}", faultyBitPosition);

                        Binary correctedFrameGeneratedVerify = GenerateVerificationBits(H, new Binary(correctedFrame.Take(columnsAmount)));
                        Binary correctedFrameVerify          = new Binary(correctedFrame.Skip(columnsAmount));
                        Binary correctionVerify = correctedFrameVerify ^ correctedFrameGeneratedVerify;
                        // if (correctionVerify.CountOnes() == 0)
                        // Console.WriteLine("Corrected frame = {0}", correctedFrame);
                        // else
                        //Console.WriteLine("The frame cannot be corrected, more than 1 bit error");
                    }
                    catch (WarningException)
                    {
                        // Console.WriteLine("The frame cannot be corrected");
                    }
                }
                if (generatedParityBit == 0)
                {
                    //Console.WriteLine("More than 1 error");
                }
            }
            else
            {
                if (generatedParityBit == 0)
                {
                    //Console.WriteLine("The received frame is correct");
                }
                if (generatedParityBit == 1)
                {
                    // Console.WriteLine("Parity bit error");
                }
            }


            // Console.ReadKey();
        }
        public String hamming_Receive(Binary received_frame)
        {
            tablica_wynikow[6, 0] = "Liczba błędów";
            tablica_wynikow[6, 1] = "0";
            Binary     wiadomosc_do_zwrotu;
            Binary     frame                          = received_frame;
            bool?      corruptMessage                 = null;
            bool       czy_wystepuja_bledy            = false;
            int        rodzaj_bledow                  = 0; // 1 - wprowadzone, 2 - wygenerowane
            List <int> lista_wpisanych_bledow         = new List <int>();
            int        liczba_bledow_do_wygenerowania = 0;
            int        frame_status                   = -1;

            // 1- ramka poprawiona
            // 2 - ramka niepoprawiona, wiecej niz 2 bledy
            // 3- ramka poprawna, brak bledow
            // -1 - blad obliczania ramki
            // 4 - blad bitu parzystosci

            if (!textBox2.Text.Equals("Brak")) // dodaj wpisane błędy
            {
                rodzaj_bledow       = 1;
                czy_wystepuja_bledy = true;
                string wpisane_bledy = textBox2.Text;
                while (wpisane_bledy.Contains(','))
                {
                    int index_przecinek = wpisane_bledy.IndexOf(',');
                    lista_wpisanych_bledow.Add(Convert.ToInt16(wpisane_bledy.Substring(0, index_przecinek)));
                    wpisane_bledy = wpisane_bledy.Substring(index_przecinek + 1);
                }
                lista_wpisanych_bledow.Add(Convert.ToInt16(wpisane_bledy));
                tablica_wynikow[6, 0] = "Liczba błędów";
                tablica_wynikow[6, 1] = lista_wpisanych_bledow.Count().ToString();
            }

            if (checkBox2.Checked == true && textBox13.Text != null) // generuj błędy
            {
                rodzaj_bledow       = 2;
                czy_wystepuja_bledy = true;
                try {
                    liczba_bledow_do_wygenerowania = Convert.ToInt16(textBox13.Text);
                    tablica_wynikow[6, 0]          = "Liczba błędów";
                    tablica_wynikow[6, 1]          = liczba_bledow_do_wygenerowania.ToString();
                }
                catch (Exception eez)
                {
                    MessageBox.Show("Błędna liczba błędów do wygenerowania");
                }
            }

            if (textBox2.Text == null && textBox13.Text == null)
            {
                czy_wystepuja_bledy = false;
            }



            int    frame_lenght  = frame.Length;
            Binary receivedFrame = new Binary(frame.ToArray());

            if (czy_wystepuja_bledy == true)
            {
                if (rodzaj_bledow == 1)
                {
                    for (int i = 0; i < lista_wpisanych_bledow.Count; i++)
                    {
                        receivedFrame[lista_wpisanych_bledow[i]] = !receivedFrame[lista_wpisanych_bledow[i]];
                    }
                }

                if (rodzaj_bledow == 2)
                {
                    for (int j = 0; j < liczba_bledow_do_wygenerowania; j++)
                    {
                        int badBit = random.Next(0, receivedFrame.Length - 1);
                        receivedFrame[badBit] = !receivedFrame[badBit];
                    }
                }

                //int badBit = random.Next(0, receivedFrame.Length - 1);
                //int badBit2 = random.Next(0, receivedFrame.Length - 1);

                //receivedFrame[badBit] = !receivedFrame[badBit2];
                //receivedFrame[badBit2] = !receivedFrame[badBit2];
            }
            Binary recivedFrameWithParitBit = new Binary(receivedFrame.ToArray().Take(frame_lenght));

            tablica_wynikow[9, 0] = "Przesłane dane";
            tablica_wynikow[9, 1] = recivedFrameWithParitBit.ToString();

            Binary receivedParityBit = new Binary(frame.ToArray().Skip(frame_lenght - 1));

            receivedFrame = new Binary(receivedFrame.ToArray().Take(frame_lenght - 1));
            Binary receivedMessage      = new Binary(receivedFrame.Take(columnsAmount));
            Binary receivedVerification = new Binary(receivedFrame.Skip(columnsAmount));
            int    generatedParityBit   = -1;

            BinaryMatrix H = GenerateHMatrix(rowsAmount, columnsAmount);
            Binary       receivedMessageVerification = GenerateVerificationBits(H, receivedMessage);
            Binary       s = receivedVerification ^ receivedMessageVerification;

            wiadomosc_do_zwrotu = new Binary(recivedFrameWithParitBit.Take(columnsAmount));
            frame_status        = 2;

            if (recivedFrameWithParitBit.CountOnes() % 2 == 0)
            {
                generatedParityBit = 0;
            }
            if (recivedFrameWithParitBit.CountOnes() % 2 != 0)
            {
                generatedParityBit = 1;
            }

            //Console.WriteLine("received frame = {0}", Binary.Concatenate(receivedFrame, receivedParityBit));
            //Console.WriteLine("H matrix:");
            //Console.Write(H);
            //Console.WriteLine("received message verification: {0}", receivedMessageVerification);
            //Console.WriteLine("s value: {0}", s);

            if (s.CountOnes() > 0)
            {
                if (generatedParityBit == 1)
                {
                    try
                    {
                        BinaryMatrix HWithIdentity     = GenerateHWithIdentity(H);
                        int          faultyBitPosition = FindFaultyBit(HWithIdentity, s);

                        tablica_wynikow[7, 0] = "Pozycja błędu";
                        tablica_wynikow[7, 1] = faultyBitPosition.ToString();

                        Binary correctedFrame = new Binary(receivedFrame.ToArray());
                        correctedFrame[faultyBitPosition] = !correctedFrame[faultyBitPosition];

                        //Console.WriteLine("H matrix with identity:");
                        //Console.Write(HWithIdentity);
                        //Console.WriteLine("Fault bit possition founded: {0}", faultyBitPosition);

                        Binary correctedFrameGeneratedVerify = GenerateVerificationBits(H, new Binary(correctedFrame.Take(columnsAmount)));
                        Binary correctedFrameVerify          = new Binary(correctedFrame.Skip(columnsAmount));
                        Binary correctionVerify = correctedFrameVerify ^ correctedFrameGeneratedVerify;
                        // if (correctionVerify.CountOnes() == 0)
                        // Console.WriteLine("Corrected frame = {0}", correctedFrame);
                        // else
                        //Console.WriteLine("The frame cannot be corrected, more than 1 bit error");
                        wiadomosc_do_zwrotu = new Binary(correctedFrame.Take(columnsAmount));
                        frame_status        = 1;
                    }
                    catch (WarningException)
                    {
                        // Console.WriteLine("The frame cannot be corrected");
                    }
                }
                if (generatedParityBit == 0)
                {
                    //Console.WriteLine("More than 1 error");
                }
            }
            else
            {
                if (generatedParityBit == 0)
                {
                    //Console.WriteLine("The received frame is correct");
                    frame_status = 3;
                }
                if (generatedParityBit == 1)
                {
                    // Console.WriteLine("Parity bit error");
                    frame_status = 4;
                }
            }
            //MessageBox.Show(frame_status.ToString());
            tablica_wynikow[8, 0] = "Ilość błędów wykrytych";
            if (frame_status == 1)
            {
                tablica_wynikow[8, 1] = "1";
            }
            if (frame_status == 2)
            {
                tablica_wynikow[8, 1] = ">2";
            }
            if (frame_status == 3)
            {
                tablica_wynikow[8, 1] = "0";
            }
            if (frame_status == 4)
            {
                tablica_wynikow[8, 1] = "Błąd bitu parzystości";
            }
            if (frame_status == -1)
            {
                tablica_wynikow[8, 1] = "Błąd obliczania";
            }

            return(wiadomosc_do_zwrotu.ToString());
        }
Beispiel #24
0
        static void PrintSegmentMap(Map map, int blueThreshold)
        {
            BinaryMatrix   discovered = new BinaryMatrix(map.Width, map.Height);
            BinaryMatrix   obstacles  = map.ScanObstacles(blueThreshold);
            List <Cluster> clusters   = new List <Cluster>();

            for (int x = 0; x < map.Width; x++)
            {
                for (int y = 0; y < map.Height; y++)
                {
                    if (!discovered[x, y] && obstacles[x, y])
                    {
                        discovered[x, y] = true;

                        Cluster    cluster = new Cluster();
                        Queue <XY> queue   = new Queue <XY>();

                        queue.Enqueue(new XY(x, y));
                        while (queue.Count > 0)
                        {
                            XY point = queue.Dequeue();
                            cluster.AddPoint(point);

                            foreach (XY neighbor in point.GetNeighbors())
                            {
                                if (
                                    neighbor.IsInsideBox(map.Box) &&
                                    !discovered[neighbor] &&
                                    obstacles[neighbor]
                                    )
                                {
                                    discovered[neighbor] = true;
                                    queue.Enqueue(neighbor);
                                }
                            }
                        }

                        clusters.Add(cluster);
                    }
                }
            }

            Console.WriteLine("Discovered {0} clusters", clusters.Count);

            Bitmap clustersMap = new Bitmap(map.Width, map.Height);

            for (int x = 0; x < map.Width; x++)
            {
                for (int y = 0; y < map.Height; y++)
                {
                    clustersMap.SetPixel(x, y, Color.Black);
                }
            }

            foreach (Cluster cluster in clusters)
            {
                for (int x = cluster.Left; x <= cluster.Right; x++)
                {
                    for (int y = cluster.Top; y <= cluster.Bottom; y++)
                    {
                        clustersMap.SetPixel(x, y, Color.Red);
                    }
                }
            }

            clustersMap.Save(FILE_BASE + "background-segments.jpg");
        }