

Town planner Ninja is assigned to build new houses in the ninja town for its citizens. The town is in the form of a rectangular grid ‘TOWN[][]’ of (‘M’ * ‘N’) dimensions where 'M' is the number of rows and 'N' is the number of columns. Each cell of the grid can be an 'empty location' (that can be used for house) denoted by '.' or 'Tree' denoted by 'T'.
Ninja wants to meet the demands of the citizens without cutting any of the trees.
Following are the conditions asked by citizens for their house locations :House should not have a house on its left cell
House should not have a house on its right cell
House should not have a house on its upper-left cell
House should not have a house on its upper-right cell
Town planner Ninja needs your help. Return the maximum number of houses that can be built while meeting the citizens' demands and not cutting any of the trees.
For Example :
Input :
M = 3 , N = 5
Grid :
. . . T .
. T . . .
T . . T .
In the example above, the maximum number of houses that be built is 8.
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then ‘T’ test cases follow:
The first line in each test case contains two space-separated positive integers ‘M’ and ‘N’, where 'M’ is the number of rows and ‘N’ is the number of columns in the grid.
The next ‘M’ lines contain a string of length ‘N’ containing ‘.’ or ‘T’ representing empty space or a Tree, respectively.
Output Format :
For each test case, print an integer denoting the maximum number of houses that can build following the conditions stated above.
Output for every test case will be printed in a separate line.
Note :
You don’t need to print anything; It has already been taken care of. Just implement the given function.
1 <= T <= 5
1 <= N <= 8
1 <= M <= 8
‘TOWN’ = { “.”,"T"}
Where 'TOWN’ denotes the 2-D grid represents whether a cell is empty or not.
Time limit: 1 sec
1
3 5
. . . T .
. T . . .
T . . T .
8
The following diagram represents the above input grid :
The possible optimal arrangement of houses is shown below:
Hence, a maximum 8 number of houses can be built if demands of citizens are to met and no tree is removed.
1
3 5
T . . . .
T . T T .
. T . . T
5
The following diagram represents the above input grid :
The possible optimal arrangement of houses is shown below:
Hence, a maximum 5 number of houses can be built if the demands of citizens are met and no tree is removed.
Can you find all valid combinations of houses in a row and call recursion to solve for other rows?
First of all, we should know why we are doing bitmasking? Below is the explanation:
Let’s now discuss the approach:
Below are the functionality of each function:
countSetBits(‘mask’) :
This function takes an integer as an argument whose set bits are to be counted.
on(‘i’ , ‘mask’) :
This function takes two integers as arguments, ‘i’ and ‘mask’ where ‘mask’ is the integer whose ‘ith’ bit is to be checked if it is set or not.
isArrangementValid(‘row’, ‘currRow’, ‘prevRow’) :
This function takes character arguments: array/list ‘row’ which is the current row, Integer ‘currRow’ which is the arrangement of houses for current row and Integer ‘prevRow’ which is the arrangement of houses in the previous row.
This function checks if ‘currRow’ is valid with respect to ‘prevRow’ an array/list ‘row’:
getMaxHouses(‘Town’, ‘prevRow’, ‘i’):
This function takes arguments: array/list of array/list of characters ‘Town’ which is the original situation of town, Integer ‘prevRow’ which previous rows arrangement of houses, Integer ‘i’ which is index of current house, and returns the best answer for ‘ith’ row with respect to ‘prevRow’ and rows after ‘i’:
Call recursion on rows after ‘i’:
planTown(‘Town’):
This function takes grid ‘Town’ as argument and calls recursive function ‘getMaxHouses’ to get the best answer, and then returns it.
O(2^M * 2^N * 2^N * N), where ‘M’ is the number of rows and ‘N’ is the number of columns.
Total number of recursive calls can be 2^’M’ * 2^’N’, and in each recursive call, 2^N arrangements are checked, where each arrangement’s validity is checked using ‘N’ iterations. Hence, total time complexity is O(2^M * 2^N * 2^N * N)
O(2^M * 2^N), where ‘M’ is the number of rows and ‘N’ is the number of columns.
Total number of recursive calls can be 2^’M’ * 2^’N’. Hence, total space taken on recursion stack is O(2^M * 2^N)