We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Brick Tiling
Brick Tiling
Sort by
recency
|
14 Discussions
|
Please Login in order to post a comment
Java Solution using memoization. The first step is to find all possible Ls that cover the grid, and create a map from grid locations to all valid Ls that cover that location (for the sake of brevity I won’t go over this part, but there’s 8 possible Ls where the ‘long bar’ starts for a single location). The most efficient way to represent the coverage of a grid this small is a bit mask (aka. an array of 3 or less longs), so it also good to idea to precompute the masks for the valid Ls and store them in a map, and also find the mask of the initial grid and fully covered grid. The last map goes from a bitmask to the number of ways you can cover that mask.
The recursive logic is pretty straightforward. Find the first open location on the current grid, and for every L you can place on this location, sum the ways you can cover the rest of the uncovered grid and that’s the number of ways you can cover the current grid. Again, this is all done with bit masks, so you’ll have to implement functions for combining masks, removing coverage from a mask, finding the first open entry and finding the coverage count of a grid (these are all very fast functions though) and a class for a bitmask. Also note that, if you combine a grid’s mask with an L’s mask, and the coverage count went up by less than 4, that means this L overlapped with some already covered part of the grid, and therefore is an invalid placement. You also have to be careful with the last long of the bit mask, as the grid is likely not a multiple of 64 (aka. not all 64 bits of the last long are in use).
Here is the main recursive function (the first call has the “to find” mask as the fully covered grid and the “currently covered” grid as the initially covered grid: ‘ public static long findWaysToCover(BitMask toFindMask, BitMask coveredMask, Map> waysToCover, Map> LsThatCover, Map lToMask, int numLeftToCover, int initialCount) {
‘
from collections import defaultdict import os
MAX_ROWS = 20 MAX_COLS = 8
def set_bit(num, count): return num | (1 << count)
def get_bit(num, i): return (num & (1 << i)) != 0
def clear_bit(num, i): mask = ~(1 << i) return num & mask
class Tile2: def init(self, row1, col1, row2, col2, row3, col3, row4, col4): self.rows = [row1, row2, row3, row4] self.cols = [col1, col2, col3, col4]
TILES = [ Tile2(0, 0, 1, 0, 2, 0, 2, 1), Tile2(0, 0, 1, 0, 1, -1, 1, -2), Tile2(0, 0, 0, 1, 1, 1, 2, 1), Tile2(0, 0, 1, 0, 0, 1, 0, 2), Tile2(0, 0, 1, 0, 2, 0, 2, -1), Tile2(0, 0, 0, 1, 0, 2, 1, 2), Tile2(0, 0, 0, 1, 1, 0, 2, 0), Tile2(0, 0, 1, 0, 1, 1, 1, 2) ]
def get_hash(arr): return ''.join(format(num, '02x') for num in arr)
cache = defaultdict(int)
def get_my_count(arr, empty_count): if empty_count % 4 != 0: return 0
def brickTiling(grid): arr = [0] * MAX_ROWS empty_count = 0 N = len(grid) M = len(grid[0])
if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')
Here is my solution in java, javascript,python, C,C++, Chsarp HackerRank Brick Tiling Problem Solution
Here is the solution of Brick Tiling Click here
Here is Brick Tiling problem solution - https://programs.programmingoneonone.com/2021/07/hackerrank-brick-tiling-problem-solution.html