Count assignments of U cells to T or D so each person's region is connected, the sizes differ by at most K, and neither region contains a 2x2 block.
Medium6BacktrackingDFSImplementationBrute forceNo attempts yetTime limit2sMemory limit512 MBTae and Dotori have one rectangular chocolate bar of size N×M. The bar is divided into 1×1 cells, and each cell has one of the letters T, D, U written on it. The two of them want to break the bar into two pieces and take one piece each. Every cell goes to one of the two people, and each person takes at least one cell.
A way of splitting the bar has to follow all of these rules.
XX
XX
Given the size of the bar and the letter written in each cell, write a program that counts the ways to split the bar into two pieces. Two ways are different when the sets of cells the two people take are different.
The first line has N, M, K (1≤N,M≤8, 0≤K≤N×M). Each of the next N lines has the letters written in the cells of one row of the bar. Each line has M characters, and each character is T, D, or U.
Print the number of ways to split the chocolate into two pieces on the first line.
If all four cells of a 2×2 bar have U and K is 4, there are 24=16 assignments. Writing T for a cell Tae takes and D for a cell Dotori takes, the 4 assignments below break the rules.
TT
TT
DD
DD
DT
TD
TD
DT
In the first two, four cells of the same person form a 2×2 square. In the last two, the cells each person takes are split apart. The other 12 follow the rules.