Recently, in a coding contest, I came across this issue.
We have 1000 tiles, where each tile is a 3x3 matrix. Each cell in the matrix has an integer value from 0 to 9, which means the height of the cell. The problem was to find the maximum pairs of tiles, such as that they fit perfectly. Tiles can be rotated to fit. in it means for tile A and tile B
A [i] + B [i] = const for i = 0 to 8
The approach I solved for this problem was that I could maintain a hash value corresponding to each tile. Then I will find possible combinations of tiles that would be a possible fit and search in the hash table.
Ex. For the tiles below
5 3 2 4 6 7 5 7 8 4 8 9 matches with 5 1 0 for const = 9 & with 6 2 1 for const=10 1 4 5 8 5 4 9 6 5
for this snippet, "const" will range from 9 (adding 0 to the maximum element) to 10 (adding 9 to the minimum element). Therefore, I would get two possible combinations for tiles, which I would look in the table.
But this method is greedy and does not give the desired answer, and also I could not think of the correct hash function, which would take into account all possible rotations.
So, what would be a good approach to solve this problem?
I am sure that there is a problem with brute force to solve this problem, but I really wondered if there is a viable solution to the problem in the line of the problem "in pairs equal to k ".
source share