Saturday, January 16, 2010

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

//  Floor Tiles.cpp : Defines the entry point for the console application.


//


 


#include "stdafx.h"


 


 


int _tmain(int argc, _TCHAR* argv[])


{
                char
A[50][50];
                int m,n,i=0,j=0,count=0,r,c;
                while(cin>>m)
                {
                                cin>>n;
                                count=0;
                                for(i=0;i<m;i++)
                                {
                                                cin>>A[i];
                                }
                                for(i=0;i<m;i++)
                                {
                                                r=0;
                       
                        for(j=0;j<n;j++)
                       
                        {
                          
                                     if(A[i][j]=='-' && r==0)
                                                                {
                                                                                count++;
                                                                                r=1;
                                                                }
                                                                else
if(A[i][j]!='-')
                                                                {r=0;}
                       
                        }
                                }
                                for(i=0;i<n;i++)
                                {
                                                c=0;
                      
                         for(j=0;j<m;j++)
                       
                        {
                         
                                      if(A[j][i]=='|' && c==0)
                                                                {
                                                                                count++;
                                                                                c=1;
                                                                }
                                                                else
if(A[j][i]!='|')
                                                                {c=0;}
                      
                         }
                                }
                                cout<<count<<"\n";
                }
                return 0;
}

4 4
----
----
----
----
4

4 4
-|--
-|--
||--
||--
8

5 5
-|-|-
|-|-|
-|-|-
|-|-|
-|-|-
25

No comments:

Post a Comment