Floor Tiles

The Problem

Given an arrangement of tiles in terms of the characters ‘-‘and ‘|’, you have to find the minimum number of tiles required to cover the whole floor under the following constraints: in a row (i,j) if there is character ‘-‘ in adjacent columns(j’s) of the same row they are the part of the same tile. Similarly if there is character ‘|’ in adjacent rows (i’s) of the same column they are the part of the same tile. Same character in different rows or columns counts a different tile. (See the examples below).

 

The Input

The first line of input will contain two integers m,n separated by white spaces representing the dimensions of the floor. Next m lines will contain n characters each representing the arrangement of the tiles.

 

The Output

For each input output the minimum number of tiles required to cover the floor in a new line.

 

Sample Input

4 4
—-
—-
—-
—-

4 4
-|–
-|–
||–
||–

Sample Output

4
8
 

using System;

using System.Collections.Generic;

using System.Text;

using System.Text.RegularExpressions;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.Write("Enter the dimensions of the floor separated by a space: ");

            string strInput = Console.ReadLine();

            Regex reg = new Regex(" ");

            int flag = 0, row = 0, col = 0;

            foreach (string str in reg.Split(strInput))

            {

                if (flag == 0)

                {

                    row = int.Parse(str);

                    flag = 1;

                }

                else

                {

                    col = int.Parse(str);

                }

            }

            char[,] floor = new char[row, col];

            for (int i = 0; i < row; i++)

            {

                for (int j = 0; j < col; j++)

                {

                    Console.Write("Enter tile pattern at position (" + (i + 1) + ", " + (j + 1) + "): ");

                    floor[i, j] = char.Parse(Console.ReadLine());

                }

            }

            //Printing the floor tiles

            for (int i = 0; i < row; i++)

            {

                for (int j = 0; j < col; j++)

                {

                    Console.Write(floor[i, j]);

                }

                Console.WriteLine();

            }

            //Counting the number of tiles

            int count = 0;

            for (int i = 0; i < row; i++)

            {

                for (int j = 0; j < col; j++)

                {

                    if (floor[i, j] == ‘-‘)

                    {

                        if (j == 0)

                        {

                            count++;

                        }

                        else if (floor[i, j – 1] != ‘-‘)

                        {

                            count++;

                        }

                        else

                        {

                            continue;

                        }

                    }

                    else

                    {

                        if (i == 0)

                        {

                            count++;

                        }

                        else if (floor[i – 1, j] != ‘|’)

                        {

                            count++;

                        }

                        else

                        {

                            continue;

                        }

                    }

                }

            }

            Console.WriteLine("Number of tiles = " + count);

        }

    }

}

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