Transforming a matrix to 0 through clicks on non-zero elements

There will be a 1000×1000 matrix containing numbers 1,2,3,4 in random positions.
A user can click on a cell with a non-zero number.
On clicking the cells containing that number, all the cells adjacent to that cell containing that number along with the current cell will become 0. Adjacent means any of the 8 cells near the current cell.

An example is given below:
Original matrix:
1 2 3 2 3
2 1 1 3 2
4 2 1 4 1
2 3 3 1 4
4 2 1 3 4
After the first click on the left-top element, the matri looks like the following:
0 2 3 2 3
2 0 0 3 2
4 2 0 4 0
2 3 3 0 4
4 2 0 3 4
In the end you have to return the number of clicks required to make the entire matrix 0.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[,] inputMatrix = new int[,]
            {
                {1, 2, 3, 2, 3},
                {2, 1, 1, 3, 2},
                {4, 2, 1, 4, 1},
                {2, 3, 3, 1, 4},
                {4, 2, 1, 3, 4}
            };
            int clickCount = 0;
            Console.WriteLine("Given Matrix");
            Console.WriteLine("============");
            for (int i = 0; i < inputMatrix.GetLength(0); i++)
            {
                for (int j = 0; j < inputMatrix.GetLength(1); j++)
                {
                    Console.Write(inputMatrix[i, j] + " ");
                }
                Console.WriteLine();
            }
            for (int i = 0; i < inputMatrix.GetLength(0); i++)
            {
                for (int j = 0; j < inputMatrix.GetLength(1); j++)
                {
                    if (inputMatrix[i, j] != 0)
                    {
                        //Call the function
                        TransformMatrix(inputMatrix, i, j, inputMatrix[i, j]);
                        clickCount++;
                        Console.WriteLine("Matrix after click: " + clickCount);
                        Console.WriteLine("=================================");
                        for (int row = 0; row < inputMatrix.GetLength(0); row++)
                        {
                            for (int col = 0; col < inputMatrix.GetLength(1); col++)
                            {
                                Console.Write(inputMatrix[row, col] + " ");
                            }
                            Console.WriteLine();
                        }
                    }
                }
            }
            Console.WriteLine("Number of clicks required to transform the entire matrix to 0 is " + clickCount);
        }

        public static void TransformMatrix(int[,] inputMatrix, int row, int col, int num)
        {
            //Set the current cell to 0
            inputMatrix[row, col] = 0;
            
            //Top left
            if (row - 1 >= 0 && col - 1 >= 0 && inputMatrix[row - 1, col - 1] == num)
            {
                TransformMatrix(inputMatrix, row - 1, col - 1, num);
            }
            
            //Top
            if (row - 1 >= 0 && inputMatrix[row - 1, col] == num)
            {
                TransformMatrix(inputMatrix, row - 1, col, num);
            }

            //Top right
            if (row - 1 >= 0 && col + 1 < inputMatrix.GetLength(1) && inputMatrix[row - 1, col + 1] == num)
            {
                TransformMatrix(inputMatrix, row - 1, col + 1, num);
            }

            //Middle right
            if (col + 1 < inputMatrix.GetLength(1) && inputMatrix[row, col + 1] == num)
            {
                TransformMatrix(inputMatrix, row, col + 1, num);
            }

            //Bottom right
            if (row + 1 < inputMatrix.GetLength(0) && col + 1 < inputMatrix.GetLength(1) && inputMatrix[row + 1, col + 1] == num)
            {
                TransformMatrix(inputMatrix, row + 1, col + 1, num);
            }

            //Bottom
            if (row + 1 < inputMatrix.GetLength(0) && inputMatrix[row + 1, col] == num)
            {
                TransformMatrix(inputMatrix, row + 1, col, num);
            }

            //Bottom left
            if (row + 1 < inputMatrix.GetLength(0) && col - 1 >= 0 && inputMatrix[row + 1, col - 1] == num)
            {
                TransformMatrix(inputMatrix, row + 1, col - 1, num);
            }

            //Middle left
            if (col - 1 >= 0 && inputMatrix[row, col - 1] == num)
            {
                TransformMatrix(inputMatrix, row, col - 1, num);
            }
        }
    }
}

Output
=======
Given Matrix
============
1 2 3 2 3
2 1 1 3 2
4 2 1 4 1
2 3 3 1 4
4 2 1 3 4
Matrix after click: 1
=================================
0 2 3 2 3
2 0 0 3 2
4 2 0 4 0
2 3 3 0 4
4 2 0 3 4
Matrix after click: 2
=================================
0 0 3 2 3
0 0 0 3 2
4 0 0 4 0
0 3 3 0 4
4 0 0 3 4
Matrix after click: 3
=================================
0 0 0 2 0
0 0 0 0 2
4 0 0 4 0
0 3 3 0 4
4 0 0 3 4
Matrix after click: 4
=================================
0 0 0 0 0
0 0 0 0 0
4 0 0 4 0
0 3 3 0 4
4 0 0 3 4
Matrix after click: 5
=================================
0 0 0 0 0
0 0 0 0 0
0 0 0 4 0
0 3 3 0 4
4 0 0 3 4
Matrix after click: 6
=================================
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 3 3 0 0
4 0 0 3 0
Matrix after click: 7
=================================
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
4 0 0 0 0
Matrix after click: 8
=================================
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Number of clicks required to transform the entire matrix to 0 is 8
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