Counting discrete clusters of 1 in a binary 2D matrix

C# code to count discrete clusters of 1 in a binary 2D matrix.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ReservoirCount
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter number of rows: ");
            string str = Console.ReadLine();
            int rows = 0;
            rows = int.Parse(str);
            Console.Write("Enter number of columns: ");
            str = Console.ReadLine();
            int cols = 0;
            cols = int.Parse(str);
            //Declaring the reservoir matrix
            int[,] reservoir = new int[rows, cols];
            int[,] reservoirCopy = new int[rows, cols];
            int[,] reservoirRotate = new int[cols, rows];
            //Reading the reservoir matrix
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    Console.Write("Enter Element(" + (i + 1) + "," + (j + 1) + "): ");
                    string digit = Console.ReadLine();
                    reservoir[i, j] = int.Parse(digit);
                    reservoirCopy[i, j] = int.Parse(digit);
                }
            }
            Display(reservoir);
            int count1 = Counter(reservoir);
            reservoirRotate = Rotate(reservoirCopy);
            int count2 = Counter(reservoirRotate);
            reservoir = Rotate(Rotate(reservoirCopy));
            int count3 = Counter(reservoir);
            reservoirRotate = Rotate(Rotate(Rotate(reservoirCopy)));
            int count4 = Counter(reservoirRotate);
            Console.WriteLine("Number of reservoirs = " + Math.Min(Math.Min(Math.Abs(count1), Math.Abs(count2)), Math.Min(Math.Abs(count3), Math.Abs(count4))));
        }
        //Transforming the reservoir matrix
        public static void Transform(int[,] reservoir, int x, int y, ref int count)
        {
            int flag = 0;
            int rows = reservoir.GetLength(0);
            int cols = reservoir.GetLength(1);
            //Top left corner
            if (x != 0 && y != 0)
            {
                if (reservoir[x - 1, y - 1] < 0)
                {
                    reservoir[x, y] = reservoir[x - 1, y - 1];
                    flag = 1;
                }
            }
            //Top corner
            if (x != 0)
            {
                if (reservoir[x - 1, y] < 0)
                {
                    reservoir[x, y] = reservoir[x - 1, y];
                    flag = 1;
                }
            }
            //Top right corner
            if (x != 0 && y != cols - 1)
            {
                if (reservoir[x - 1, y + 1] < 0)
                {
                    reservoir[x, y] = reservoir[x - 1, y + 1];
                    flag = 1;
                }
            }
            //Bottom left corner
            if (x != rows - 1 && y != 0)
            {
                if (reservoir[x + 1, y - 1] < 0)
                {
                    reservoir[x, y] = reservoir[x + 1, y - 1];
                    flag = 1;
                }
            }
            //Bottom corner
            if (x != rows - 1)
            {
                if (reservoir[x + 1, y] < 0)
                {
                    reservoir[x, y] = reservoir[x + 1, y];
                    flag = 1;
                }
            }
            //Bottom right corner
            if (x != rows - 1 && y != cols - 1)
            {
                if (reservoir[x + 1, y + 1] < 0)
                {
                    reservoir[x, y] = reservoir[x + 1, y + 1];
                    flag = 1;
                }

            }
            //Left corner
            if (y != 0)
            {
                if (reservoir[x, y - 1] < 0)
                {
                    reservoir[x, y] = reservoir[x, y - 1];
                    flag = 1;
                }
            }
            //Right corner
            if (y != cols - 1)
            {
                if (reservoir[x, y + 1] < 0)
                {
                    reservoir[x, y] = reservoir[x, y + 1];
                    flag = 1;
                }
            }
            if (flag == 0)
            {
                count--;
                reservoir[x, y] = count;
            }
        }
        //Counting number of reservoirs
        public static int Counter(int[,] reservoir)
        {
            int count = 0;
            int rows = reservoir.GetLength(0);
            int cols = reservoir.GetLength(1);
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    if (reservoir[i, j] == 1)
                    {
                        Transform(reservoir, i, j, ref count);
                    }
                    //Top left corner
                    if (i != 0 && j != 0)
                    {
                        if (reservoir[i - 1, j - 1] == 1)
                        {
                            Transform(reservoir, i - 1, j - 1, ref count);
                        }
                    }
                    //Top corner
                    if (i != 0)
                    {
                        if (reservoir[i - 1, j] == 1)
                        {
                            Transform(reservoir, i - 1, j, ref count);
                        }
                    }
                    //Top right corner
                    if (i != 0 && j != cols - 1)
                    {
                        if (reservoir[i - 1, j + 1] == 1)
                        {
                            Transform(reservoir, i - 1, j + 1, ref count);
                        }
                    }
                    //Bottom left corner
                    if (i != rows - 1 && j != 0)
                    {
                        if (reservoir[i + 1, j - 1] == 1)
                        {
                            Transform(reservoir, i + 1, j - 1, ref count);
                        }
                    }
                    //Bottom corner
                    if (i != rows - 1)
                    {
                        if (reservoir[i + 1, j] == 1)
                        {
                            Transform(reservoir, i + 1, j, ref count);
                        }
                    }
                    //Bottom right corner
                    if (i != rows - 1 && j != cols - 1)
                    {
                        if (reservoir[i + 1, j + 1] == 1)
                        {
                            Transform(reservoir, i + 1, j + 1, ref count);
                        }
                    }
                    //Left corner
                    if (j != 0)
                    {
                        if (reservoir[i, j - 1] == 1)
                        {
                            Transform(reservoir, i, j - 1, ref count);
                        }
                    }
                    //Right corner
                    if (j != cols - 1)
                    {
                        if (reservoir[i, j + 1] == 1)
                        {
                            Transform(reservoir, i, j + 1, ref count);
                        }
                    }
                }
            }
            return count;
        }
        //Rotate of matrix
        public static int[,] Rotate(int[,] reservoir)
        {
            int rows = reservoir.GetLength(0);
            int cols = reservoir.GetLength(1);
            int[,] reservoirRotate = new int[cols, rows];
            for (int j = 0; j < cols; j++)
            {
                for (int i = rows - 1; i >= 0; i--)
                {
                    reservoirRotate[j, rows - 1 - i] = reservoir[i, j];
                }
            }
            return (reservoirRotate);
        }
        //Matrix Display
        public static void Display(int[,] tempMatrix)
        {
            int rows = tempMatrix.GetLength(0);
            int cols = tempMatrix.GetLength(1);
            //Displaying the reservoir matrix
            Console.WriteLine("Displaying reservoir matrix:");
            Console.WriteLine("============================");
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    Console.Write(tempMatrix[i, j]);
                    Console.Write(" ");
                }
                Console.WriteLine();
            }
            Console.WriteLine("============================");
        }
    }
}

Output
=======
Enter number of rows: 3
Enter number of columns: 5
Enter Element(1,1): 1
Enter Element(1,2): 1
Enter Element(1,3): 1
Enter Element(1,4): 0
Enter Element(1,5): 0
Enter Element(2,1): 0
Enter Element(2,2): 0
Enter Element(2,3): 1
Enter Element(2,4): 0
Enter Element(2,5): 1
Enter Element(3,1): 0
Enter Element(3,2): 1
Enter Element(3,3): 0
Enter Element(3,4): 1
Enter Element(3,5): 1
Displaying reservoir matrix:
============================
1 1 1 0 0
0 0 1 0 1
0 1 0 1 1
============================
Number of reservoirs = 1
Press any key to continue . . .
Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s