We need to compute the number of tilings of an H x W grid (H rows, W columns) with dominoes of size 1x2 or 2x1? Wait: tatami mats are rectangular with aspect ratio 1:2, so they are 1x2 rectangles. The condition: "it is prohibited to make a cross with the border of the tatami mats." This means we cannot have a configuration where four mats meet at a point? Actually typical tatami puzzle: tatami mats are arranged such that no three mats share a corner? Let's parse: "When spreading tatami mats on a floor, it is prohibited to make a cross with the border of the tatami mats, because it is believed to bring bad luck." In Japanese tatami layout, a "cross" would be where the boundaries of four mats meet at a point, forming a plus shape (a crossing of seams). In tatami laying, it's traditional to avoid having four corners meet at a point; instead, they prefer only three corners meeting (like a T-junction). This is a known constraint: tatami tilings are domino tilings (2:1 rectangles) with the additional restriction that no four tiles meet at a point. Equivalently, the tiling is a "monomer-dimer"? Actually it's just a domino tiling of a grid, but with the condition that the "floorplan" must be a tatami tiling, i.e., no point where four domino corners meet. That's a known combinatorial object: "tatami tilings" of a rectangle with 1x2 tiles and the condition that no four tiles meet at a point.
But wait, the problem statement: "a tatami mat, a Japanese traditional floor cover, has a rectangular form with aspect ratio 1:2. When spreading tatami mats on a floor, it is prohibited to make a cross with the border of the tatami mats, because it is believed to bring bad luck." So indeed, the prohibited pattern is the crossing of borders. That is exactly the condition that no four tiles meet at a point. In terms of grid tiling, if we place 1x2 dominoes on unit squares, corners meet at grid points. A cross occurs when four tiles meet at a grid point interior to the rectangle (i.e., a point where both horizontal and vertical seams cross). In a domino tiling, each interior grid point is adjacent to up to 4 unit squares (north, south, east, west). A cross would be when all four surrounding squares are covered by dominoes that all have that point as a corner, which means that the four squares are paired in such a way that the point is a meeting of four corners? Actually careful: In a domino tiling, each domino covers two adjacent unit squares (either horizontally or vertically). The corners of dominoes are at integer lattice points. For an interior lattice point (i,j) (with 1 ≤ i ≤ H-1, 1 ≤ j ≤ W-1), there are four unit squares around it: (i,j) (i+1,j) (i,j+1) (i+1,j+1) in some indexing. The dominoes covering these squares may have that point as a corner. The "cross" would be when the seams from all four tiles meet at that point. But if the point is interior, each adjacent square belongs to a domino. The condition of no cross means that not all four squares can have their tile edges meeting at that point. However, in a domino tiling, the meeting of four corners is possible? Let's consider a typical grid tiled by dominoes: If we have a 2x2 block all covered by two horizontal dominoes (top row two squares covered by one horizontal domino covering (1,1)-(1,2), and bottom row similarly one horizontal covering (2,1)-(2,2)), then the interior point at (1,1)? Actually coordinates: Let's define grid points (0..H, 0..W). The interior point (i,j) for i=1..H-1, j=1..W-1. The four squares are: (i,j) (southwest?), but consistent: Suppose we index rows 1..H, columns 1..W. Square (i,j) is top-left? Usually we think of coordinates from top-left? But anyway: For point between four squares: squares (i,j), (i,j+1), (i+1,j), (i+1,j+1) if rows increase downward. For a 2x2 block with two horizontal dominoes: Top row: domino covers (1,1)-(1,2) (so squares (1,1) and (1,2)); bottom row: domino covers (2,1)-(2,2). Then the interior point at (1,1) (between rows1-2 and cols1-2) is a corner of all four squares? The top-left square (1,1) has its bottom-right corner? Actually each square has four corners. The interior point is the bottom-right corner of square (1,1), bottom-left of (1,2), top-right of (2,1), top-left of (2,2). For each domino, its corners: the horizontal domino covering (1,1)-(1,2) has corners at that interior point? That domino's bottom edge runs along the line between rows1-2, so its bottom-left corner is at (col1, row2)?? Let's be systematic: The domino covers two adjacent squares. Its shape is a union of two unit squares. The boundary of the domino includes the outer boundary of those two squares, but the interior seam between the two squares is not considered a border? Wait: In the tatami layout, the borders of the mats are the edges between mats and also the outer perimeter. The prohibition is about making a cross with the border of the tatami mats. That means that at an interior point, four mat borders meeting would form a cross. This occurs when the layout has a point where the seams between mats form a plus shape. This is equivalent to having four corners of four different mats meeting at a point. But in domino tilings, two squares that belong to the same domino share an edge; that edge is inside the domino, not a border between mats. The borders between mats are the edges between squares that belong to different mats. So we need to consider the arrangement of mats (dominoes) and the resulting pattern of mat boundaries (the edges between adjacent squares that belong to different mats, plus the outer boundary). The forbidden cross is when four such boundaries meet at a point, i.e., the point is the meeting of four edges that separate mats. This happens when the four squares around a point belong to four different mats, and the borders between them form a cross.
In a domino tiling, each square belongs to a domino. For an interior lattice point (between four squares), there are four edges emanating from it: north, south, east, west? Actually the "border" lines are the boundaries between squares. At the point, there can be up to four incident edges that are boundaries between mats. The cross would be if all four such edges are present as mat borders. That would require that the two squares to the north and south belong to different mats (so there is a vertical boundary crossing the point), and similarly the two squares to the east and west belong to different mats (so there is a horizontal boundary crossing the point). But careful: The edges at the point: consider the vertical line through the point between two columns? The point is at the intersection of a grid line between columns j and j+1 and between rows i and i+1. The vertical border segment just above the point? Actually typical cross: a plus sign formed by the borders. At the grid intersection, there are four possible border segments: north (between squares (i,j+1) and (i+1,j+1)?) I'm mixing.
Better to model as a grid of unit cells. The tatami mats are 1x2 dominoes. The borders between mats are the edges that separate two cells that belong to different mats. Additionally, edges on the perimeter are borders by default, but they don't cause cross because they are at boundary.
The condition "no cross" means that there is no interior grid point (where four cells meet) such that there is a vertical border line crossing that point (i.e., the two cells to left and right of the vertical line are in different mats) and a horizontal border line crossing that point (i.e., the two cells above and below the horizontal line are in different mats). In other words, at any interior vertex, not both of the following hold: the cells to the east and west (i.e., to the right and left) belong to different mats (so there's a vertical border passing through that point) AND the cells to the north and south belong to different mats (so there's a horizontal border passing through that point). Equivalently, the four cells around the point cannot all belong to distinct mats in a way that creates a cross.
But we need to be precise: Suppose we have four cells: top-left (i,j), top-right (i,j+1), bottom-left (i+1,j), bottom-right (i+1,j+1). The point is the common corner among them. There is a potential vertical border along the line between columns j and j+1 that runs between the top-left and top-right cells and between bottom-left and bottom-right cells. However, a border is only drawn if the two cells on either side belong to different mats. So if top-left and top-right are in different mats, then there is a border segment along the top half of that vertical line. Similarly, if bottom-left and bottom-right are in different mats, there is a border segment along the bottom half. At the point, these two segments may meet, but do they form a cross? The cross would require both vertical and horizontal borders meeting at the point. The horizontal border runs along the line between rows i and i+1, separating top-left from bottom-left (left side) and top-right from bottom-right (right side). If top-left and bottom-left are in different mats, then there is a horizontal border segment on the left half. If top-right and bottom-right are in different mats, there is a horizontal border segment on the right half. For a full cross, we need that at that point, all four surrounding cells are pairwise different across both axes. Typically, the cross occurs when there is a continuous vertical border line crossing the horizontal line, and a continuous horizontal border line crossing the vertical line, forming a plus shape. That requires that the vertical border exists on both sides of the point (i.e., both the top and bottom pairs are different) and the horizontal border exists on both sides (both left and right pairs are different). But is it necessary that both top and bottom pairs be different? If only one side has a border, the vertical line would not go through the point; it would be a T-junction or L shape. The classic tatami condition is that no point has four tiles meeting, which is exactly the condition that at any interior grid point, the four cells around it are not all from four different mats. Actually, let's think: If the four cells belong to three mats, e.g., top-left and bottom-left are the same mat (vertical domino covering two cells) and top-right and bottom-right are different mats, then at the point, we may have a border between left and right across the vertical line? But the left and right cells might be from different mats? Wait, the vertical border between columns j and j+1: the top half border exists if top-left and top-right are different mats; the bottom half exists if bottom-left and bottom-right are different mats. If top-left=bottom-left (same mat), and top-right is different from that mat, then top-left vs top-right are different → border on top half; bottom-left vs bottom-right: bottom-left (same as top-left) vs bottom-right (maybe same as top-right or different) — if bottom-right is different from bottom-left, then border on bottom half. So both halves could have borders, giving a continuous vertical line. Similarly, horizontal border: top-left vs bottom-left: they are same mat → no border on left half; top-right vs bottom-right: if different, then border on right half. So there would be a vertical line through the point, but only a horizontal line on the right side, not left, so the intersection would be a T shape, not a plus. That is allowed? I think the tatami rule forbids a cross (four tiles meeting), but a T-junction (three tiles meeting) is allowed. So indeed, the condition is that at no interior grid point do all four adjacent cells belong to four different mats (or more precisely, that there is both a vertical and horizontal border through that point). However, consider case where top-left and top-right are different, bottom-left and bottom-right are different, and also top-left and bottom-left are different, and top-right and bottom-right are different. Then all four cells are pairwise different across both axes, giving a plus. That's forbidden. If any pair across an edge are the same mat, then the border on that edge segment is absent, so no plus.
Thus, the forbidden configuration is: For some interior grid point (i,j) (1 ≤ i < H, 1 ≤ j < W), we have:
- The two cells to the north (i, j) and south (i+1, j) are in different mats; AND
- The two cells to the east (i, j+1) and west (i+1, j+1)? Wait careful: For horizontal border: the cells above and below the point on the left column: (i,j) and (i+1,j) are different; and on the right column: (i,j+1) and (i+1,j+1) are different. But actually for a continuous horizontal line through the point, we need the border on both sides of the point? The horizontal line runs left-right through the point. The border is present on the left side if the cells to left of the line (i.e., (i,j) and (i+1,j) are different? Actually the horizontal line is between rows i and i+1. That line separates the top cells from bottom cells. At a given column, there is a border segment if the top cell and bottom cell in that column belong to different mats. For the horizontal line to cross the point, we need that on both the left side and right side of the point (i.e., on both columns j and j+1) there is a border? Actually the point is at the intersection of the vertical grid line between columns j and j+1 and the horizontal grid line between rows i and i+1. For a plus sign, we need both the vertical line and the horizontal line to be present and crossing. The vertical line is the line between columns j and j+1; it exists as a border (i.e., is drawn) if for all rows that the line passes through, the adjacent cells on left and right are in different mats? But the line is continuous; it may be broken if at some row the adjacent cells are same mat. For the vertical line to pass through the point, we need that both the top half (between rows i and i+1, but actually the vertical line segment above the point goes from the point up to the next point? Wait, the vertical line runs from the boundary or from another intersection. At the specific point, for the vertical line to be present at that point, we need that the cells immediately to the left and right of the vertical line at that row interval on both sides of the point are different? Actually the point is a vertex; the vertical line crossing the point consists of two segments meeting at the point: one segment above (between the point and the vertex above) and one segment below (between the point and the vertex below). The presence of the vertical line above the point is determined by the two cells adjacent on that segment: those would be the cells (i, j) and (i, j+1) (top row) being different? Let's define: The vertical grid line between columns j and j+1 runs from y=0 to y=H (if rows index 0..H). The segment between row i and i+1 (i from 0 to H-1) is present if the two cells that share that edge are in different mats: cell at (i+1? careful with indexing). Usually, we index cells by rows 1..H, columns 1..W. The vertical edge between cell (r, c) and cell (r, c+1) is at column boundary c+0.5? But easier: For integer lattice points, the edges are between them. But we consider the point as intersection of grid lines. The vertical line through that point is continuous if for both row intervals adjacent to the point, the cells on left and right are different.
Alternatively, we can translate the tatami condition into a condition on the domino tiling: No four tiles meet at a point. In a domino tiling of a grid by 1x2 dominoes, a meeting of four tiles at a point occurs exactly when the tiling contains a 2x2 square that is covered by two dominoes that are both oriented the same way? Let's test: If we have two horizontal dominoes covering rows 1 and 2 in a 2x2 block, then at the center point, we have four tile corners meeting? The four tiles: each domino is a tile; but there are only two tiles, each covering two squares. So at the center point, there are only two tiles meeting, not four. Actually each domino covers two squares; in that configuration, the top horizontal domino covers top-left and top-right squares; its bottom edge runs along the horizontal line between rows 1 and 2, but at the central point, the bottom edge of the top domino is a straight line; the bottom domino's top edge is also along that line. So the two dominoes meet along the horizontal line. At the central point, there is no vertical border because the left and right squares belong to the same domino? Actually the vertical line between columns 1 and 2: top-left and top-right are part of the same domino, so no border; bottom-left and bottom-right are part of the same domino, so no border. So there is no cross. So that's allowed.
If we have one vertical domino covering left two squares, and one vertical domino covering right two squares, then we have two vertical dominoes. At the center, the vertical line between left and right has borders? Left and right are different dominoes, so there is a vertical border. The horizontal line between top and bottom: top-left and bottom-left are same domino -> no border; top-right and bottom-right are same domino -> no border. So vertical line present, but horizontal not, so T shape? Actually vertical line runs through the point, but horizontal line absent, so it's like a vertical line crossing a horizontal grid line, but not a border. That would appear as a vertical border crossing a horizontal seam? But since horizontal seam absent, it's just a vertical border crossing the grid line; at the point, there is a vertical border and two corners of tiles meeting? That might be considered a "cross"? I'm not entirely sure. Typically, in tatami tilings, the prohibited configuration is where four corners meet. In this case, we have two vertical dominoes; at the center, we have the corners of four squares meeting, but those squares belong to two tiles: the left domino (covers two squares) and the right domino (covers two). At the center point, the left domino contributes two corners (from its two squares) and the right domino contributes two corners, so four corners from two tiles meet. Is that considered a cross? The phrase "make a cross with the border" suggests the borders themselves form a cross. In the two vertical dominoes case, there is a vertical border between left and right, but no horizontal border. So the borders form a T? Actually at the center, the vertical border line crosses the horizontal grid line, but the horizontal line is not a border (since both top and bottom squares on each side are same domino). So the border lines form a "T"? The vertical border goes through the point, but there is no horizontal border. The lines meeting are: vertical border line, and the horizontal grid line (which is not a border, but it's the seam between rows? Actually the horizontal grid line is still present as a line between rows, but if it's not a border between mats, then it's not a visible seam? In tatami layout, the borders are only where mats meet. So the seam between two cells belonging to the same mat is not a border. So at that point, only the vertical border line exists; it meets the point, but there is no horizontal border line. So it's not a cross. So allowed.
If we have a 2x2 block covered by one horizontal domino on top and one vertical domino on the left? Let's consider: top row: a horizontal domino covering both squares, so top-left and top-right same mat. bottom row: suppose bottom-left is part of a vertical domino that also includes top-left? That would be overlapping; can't. So we need a tiling. A possible configuration: a vertical domino covers left column (top-left and bottom-left), and a horizontal domino covers bottom row (bottom-left and bottom-right) would overlap bottom-left. So not allowed. So the only ways to tile a 2x2 with dominoes are either two horizontal or two vertical or one L-shaped tromino? But dominoes must cover exactly two squares, so a 2x2 area can be tiled either by two horizontal dominoes or two vertical dominoes. So that's it. So typical domino tilings never produce a configuration where four different dominoes meet at a point? Because in a domino tiling, any 2x2 block can contain at most two dominoes. To have four different dominoes meet, we would need each of the four squares to belong to a different domino. That is impossible in a domino tiling? Actually is it possible? Consider a larger tiling: imagine a 3x3 with some pattern. Could there be an interior point where four squares from four different dominoes meet? That would require that none of the four squares are paired with each other in a domino; they are each paired with some other square outside this 2x2 block. For example, a 2x2 block where each square is part of a domino that extends outward. This is possible. For instance, consider a 4x4 board. Place a vertical domino in top-left covering (1,1)-(2,1); a vertical domino in top-right covering (1,2)-(2,2)?? Actually careful: Let's label cells (r,c). Consider the 2x2 block with cells (1,1), (1,2), (2,1), (2,2). We want each of these four cells to belong to a different domino, and none of these dominoes pair two cells within the block. So each domino must pair one cell inside the block with a cell outside. For (1,1) could be paired with (1,0) (left) if exists, or (0,1) (above), or (1,2)? but that would be within block, not allowed. Similarly for others. So we need the board to be larger. Example: a 3x3 board: cells (1,1) (1,2) (1,3); (2,1) (2,2) (2,3); (3,1) (3,2) (3,3). Consider interior point between rows1-2 and cols1-2, i.e., between cells (1,1),(1,2),(2,1),(2,2). To have four different dominoes: (1,1) paired with something outside this 2x2, say (1,0) if that exists (but board boundary). But if we are inside, we need adjacency. (1,1) could pair with (1,2) but that would be within block. Could pair with (2,1) also within block. Could pair with (1,0) outside left boundary? Not possible if at boundary. So to have an interior point, we need at least a 4x4 or larger to allow extensions. For example, take a 4x4 board. Let's try to create a cross at the central point between rows 2 and 3 and columns 2 and 3 (i.e., cells (2,2),(2,3),(3,2),(3,3)). We want each of these four cells to belong to a different domino, none pairing within this block. Possible tiling:
- (2,2) paired with (2,1) (horizontal left)
- (2,3) paired with (1,3) (vertical up)
- (3,2) paired with (4,2) (vertical down)
- (3,3) paired with (3,4) (horizontal right)
That yields a cross at the interior point. Check if dominoes overlap? (2,2)-(2,1) is horizontal covering (2,1)-(2,2). (2,3)-(1,3) vertical covering (1,3)-(2,3). (3,2)-(4,2) vertical covering (3,2)-(4,2). (3,3)-(3,4) horizontal covering (3,3)-(3,4). All are distinct and non-overlapping. This covers a 4x4 board? Not fully, but we need to tile entire board. This pattern could be part of a larger tiling. So such a cross is indeed possible in a domino tiling. And it's prohibited.
Thus the tatami condition is exactly that no interior grid point has all four incident squares belonging to four distinct dominoes, which is equivalent to forbidding the pattern described.
Now the problem: count the number of tilings of H x W board with 1x2 dominoes (unrotated? They can be placed horizontally (1x2) or vertically (2x1) because aspect ratio 1:2, so both orientations allowed) such that the tatami condition holds (no cross). H and W up to 20, but product up to 400. That's a moderate size, but counting tilings with constraints is combinatorial. We need to compute number of valid tilings.
We can think of this as a constrained domino tiling. This is similar to counting "tatami tilings" of a rectangle. There is known combinatorial results: The number of tatami tilings for an n x m grid? Possibly known as "dimer coverings with no 'cross'"? Not sure.
Given small constraints (≤20), we could do DP with state compression. Since H and W up to 20, we can choose the smaller dimension as width for DP over rows. The state would need to capture the tiling of current row and the constraint across rows? The tatami condition involves 2x2 blocks. It can be enforced locally by checking at each grid point after placing dominoes.
Standard domino tiling DP (state: which cells are filled by dominoes extending from previous row) can be extended with additional constraints.
But the tatami condition is about four squares meeting at a point, i.e., at each interior vertex, we must avoid a configuration where all four incident cells are from different dominoes. This can be expressed as: For any 2x2 block of cells, it must not be the case that each of the four cells is paired with a cell outside that 2x2 block. Equivalently, in any 2x2 block, at least two of the cells must be covered by the same domino (i.e., they are paired together). Because if all four are separate, then the vertex at the center of the block would have a cross. Actually is that sufficient? Let's verify: The cross occurs at the interior point between four cells if none of the four pairs of cells sharing an edge across that point are part of the same domino. More precisely: At the point between cells A (top-left), B (top-right), C (bottom-left), D (bottom-right), the cross appears if A and B are in different dominoes (so vertical border on top), C and D are in different dominoes (vertical border on bottom), A and C are different (horizontal border on left), B and D are different (horizontal border on right). This implies that all four cells belong to distinct dominoes? Could there be a scenario where A and D are same domino? That's not adjacent; they are diagonal, not sharing an edge, so they could be same domino? But dominoes cover only two adjacent cells, so A and D cannot be in the same domino because they are not orthogonally adjacent. So if A and D were the same domino, they'd have to be connected via a path? No, a domino covers exactly two cells, so they cannot be diagonally adjacent because that would require covering two cells that are not adjacent. So each domino covers a pair of orthogonal adjacent cells. Therefore, if any two of the four cells belong to the same domino, they must be adjacent (share an edge). The possible pairs that could be in the same domino are (A,B) horizontal top, (C,D) horizontal bottom, (A,C) vertical left, (B,D) vertical right. Also (A,D) or (B,C) cannot be same domino because they are diagonal. So the condition that at least one of these four adjacent pairs is covered by the same domino ensures that not all four are distinct. Conversely, if all four are distinct, none of these four pairs are in the same domino. So the cross is equivalent to: In the 2x2 block, all four cells belong to four different dominoes, i.e., no domino lies entirely within the block, and also no domino crosses the block boundary? Actually if a domino includes one cell inside and one outside, that's fine, but if two cells inside are paired, that's a domino inside the block. The cross condition is exactly that there is no domino covering any of the four internal edges (top horizontal, bottom horizontal, left vertical, right vertical) of the 2x2 block. That is, all four internal edges (between A-B, C-D, A-C, B-D) are cuts between different dominoes. So if at least one of those edges is internal to a domino (i.e., the two cells are part of the same domino), then that edge is not a border, thus no cross.
Hence, the tatami condition can be enforced by: For every 2x2 block of cells, it is not allowed that all four cells are covered by dominoes that do not pair any adjacent cells within that block. In other words, each 2x2 block must contain at least one domino that lies entirely within that block (i.e., either a horizontal domino covering the two top cells or the two bottom cells, or a vertical domino covering the two left cells or the two right cells). Wait, a vertical domino covering left two cells (A and C) lies within the block, yes. Similarly horizontal domino covering top two cells (A and B) lies within block. But what about a domino that lies across the boundary of the block? For example, a horizontal domino covering B and some cell to the right (outside block) would not create an internal edge within the block, so that does not prevent cross. So the condition is that at least one of the four internal edges is part of a domino. That is equivalent to: In each 2x2 block, we must not have the pattern where all four internal edges are borders (i.e., are between different dominoes). So we need to avoid that.
So this is a local constraint involving 2x2 blocks. It can be enforced during DP as we fill the grid row by row.
Standard domino tiling DP (like for counting tilings with dominoes) uses a state representing which cells in the current row are already covered by a domino extending from the previous row (i.e., vertical dominoes that started in previous row and continue into current row). Then we place horizontal dominoes and start new vertical dominoes in the current row, proceeding column by column. The DP state is a bitmask of width W, where a 1 indicates that the cell in the current row is already filled (by a vertical domino from above), and 0 indicates empty (to be filled by either a horizontal domino starting here or the top of a vertical domino). This is typical for counting number of domino tilings (which is the number of perfect matchings in grid graph). That DP has complexity O(H * W * 2^W) which for W <= 20 yields about 20202^20 = 20201,048,576 ≈ 419 million, which might be borderline but often optimized with pruning and using smaller dimension as width (choose min(H,W) as width). Since max 20, 2^20 ~ 1M, and H up to 20, DP with 1M states per row is doable if optimized in C++ but in Python might be heavy. But we can maybe reduce by using memoization and transitions column by column. However we need additional constraint about 2x2 blocks.
We can incorporate the cross condition as we fill cells. The condition involves 2x2 blocks, so as we place dominoes we can check for any 2x2 block that becomes fully determined (all four cells placed) whether it violates. In row-by-row DP, when we finish a row, we have placed all cells up to that row. But the condition involves blocks that span two rows. So after placing each row, we can check the 2x2 blocks that have both rows fully assigned (i.e., current row and previous row). For a given column, the block consisting of previous row cells (row r-1, col c and c+1) and current row cells (row r, col c and c+1) must satisfy the condition. So as we complete a row, we can verify that for all c from 0 to W-2, the 2x2 block formed by rows r-1 and r, columns c and c+1 is valid (i.e., not a cross). This condition can be expressed in terms of the domino placements in those cells.
Thus, we can perform DP over rows, with state including the assignment of domino types for the previous row? But typical DP for domino tiling only tracks which cells are occupied from above, not the full domino connectivity. However, to check the cross condition, we need to know, for each cell in the previous row, which other cell it is paired with (i.e., whether it is part of a horizontal domino within that row or part of a vertical domino with the row above). In the standard DP, when we finish a row, we have determined for each cell in that row whether it was covered by a horizontal domino (pairing with neighbor in same row) or by the top of a vertical domino (i.e., a new vertical domino started in this row and will extend downward). Actually the typical DP processes row by row, column by column, and when it finishes a row, all cells of that row are covered. The state for the next row is a bitmask indicating which cells are already covered by the bottom part of vertical dominoes that started in the previous row. That bitmask tells us: for each column, if the cell in the next row is already occupied (by the vertical domino from above), then that cell is not to be filled in the next row. However, to check the cross condition on 2x2 blocks between row r-1 and r, we need to know for each cell in row r-1 whether it is paired horizontally (with left/right) or vertically (with row r). That information can be deduced from the DP when we finished row r-1: the way we placed dominoes gave us a pattern. But in the standard DP, after finishing row r-1, we only retain the bitmask for the next row (which indicates which cells are already filled from above). That bitmask essentially tells us which cells in row r-1 were the top of vertical dominoes (since those vertical dominoes extend into row r, making those cells in row r occupied). However, it does not tell us about horizontal domino placements in row r-1. Those horizontal placements are internal to row r-1 and don't affect the next row's occupancy. But to check cross condition for the 2x2 blocks between rows r-1 and r, we need to know, for each cell in row r-1, whether it was part of a horizontal domino or vertical domino. Because the condition involves whether the internal edges (vertical edges within row r-1 between columns, and vertical edges within row r, and horizontal edges between rows) are borders.
Specifically, consider a 2x2 block with cells:
A = (r-1, c)
B = (r-1, c+1)
C = (r, c)
D = (r, c+1)
We need to avoid the configuration where:
- A and B are in different dominoes (i.e., the edge between them is a border)
- C and D are in different dominoes
- A and C are in different dominoes
- B and D are in different dominoes
But note: A and C are adjacent vertically; they could be part of the same domino if that domino is vertical covering A and C. In the DP, if A is the top of a vertical domino, then A and C are same domino (C is the bottom). In that case, the vertical edge between them is not a border (it's internal to the domino). Similarly, if B and D are part of a vertical domino, then that edge is internal.
If A and B are part of a horizontal domino within row r-1, then the top edge between them is internal.
If C and D are part of a horizontal domino within row r, then the bottom edge is internal.
Thus, to evaluate the condition, we need to know for each cell in the previous row, whether it is paired horizontally (and with which neighbor) or vertically (with the cell below). And for the current row, we need to know similar information, but we might not have fully determined horizontal pairings in the current row when we check? However, we can check the condition after the current row is fully placed. At that point, we know the domino placements for both rows, so we can check all blocks between them.
But in DP, we generate tilings row by row. After we finish placing row r, we know the complete tiling for rows up to r. We can then check the 2x2 blocks between row r-1 and r. If any block violates, we discard that state. Then we proceed to next row.
Thus, we need to maintain enough information in the DP state to later check these blocks. The typical state only stores which cells in row r are already occupied by vertical dominoes from row r-1 (i.e., the bitmask of occupied cells in row r before we start placing new dominoes in row r). That bitmask, together with the way we place dominoes in row r, determines the domino placements in row r. However, when we later move to row r+1, we need to know, for row r, which cells are the tops of vertical dominoes (i.e., started in row r and will extend to row r+1). That's exactly the information we will propagate as the next state: the cells in row r that are covered by the top part of vertical dominoes, i.e., those that we started vertical dominoes from row r, which will cause the corresponding cells in row r+1 to be occupied.
But to check the cross condition for blocks between rows r-1 and r, we need to know for row r-1 whether each cell was part of a horizontal domino or vertical. That information is not fully captured by the bitmask alone because horizontal placements are not recorded. However, we can reconstruct the horizontal placements from the way we placed row r-1 when we processed it, but in the DP we are summarizing many tilings that lead to the same occupancy mask for the next row, but they may have different horizontal placement patterns. The cross condition depends on those horizontal placements (in row r-1) as well as vertical placements (which are encoded partly in the mask? Actually vertical placements: if a cell in row r-1 is the top of a vertical domino, then the corresponding cell in row r is occupied (mask bit = 1). If it is not the top, then either it was covered as the bottom of a vertical domino from row r-2 (then mask bit from previous row would be 1 indicating it's occupied from above, so we didn't place anything) or it was covered by a horizontal domino in row r-1. But when we are at row r, we have already placed row r-1 completely. The mask that we used to start row r tells us which cells in row r are already occupied from above (i.e., are the bottom parts of vertical dominoes from row r-1). Those correspond exactly to the cells in row r-1 that were the tops of vertical dominoes. So from the mask we can deduce which cells in row r-1 were vertical tops. Specifically, if the mask for row r (before placing row r) has a 1 in column c, that means cell (r, c) is already filled by a vertical domino from (r-1, c). Thus, cell (r-1, c) must have been the top of that vertical domino. So we know that for each column, if mask bit at row r is 1, then (r-1, c) is vertical top; otherwise, (r-1, c) is either covered by a horizontal domino or is the bottom of a vertical from row r-2? Wait: When we processed row r-1, we started with a mask that told us which cells were already occupied from row r-2. In row r-1, we placed dominoes to cover the remaining empty cells. At the end of row r-1, we produced a new mask for row r indicating which cells in row r are occupied by vertical dominoes started in row r-1. So the state transition takes an incoming mask (occupied cells in row r-1 from above) and generates an outgoing mask (occupied cells in row r from row r-1) along with the placements in row r-1 (horizontal or vertical). But the DP that only tracks masks lumps together different horizontal placement patterns that lead to the same outgoing mask. That's fine for counting tilings without additional constraints, because the cross condition may depend on the horizontal placements, so we cannot lump them together if they affect validity. Therefore, we need to extend the state to include information about the horizontal domino placements in the previous row, at least enough to evaluate the cross condition with the next row.
Specifically, to check the 2x2 blocks between row r-1 and r, we need to know for row r-1, for each column, whether the cell is part of a horizontal domino to the right or left, or is vertical top, or is the bottom of a vertical from above. But the bottom of vertical from above is indicated by the incoming mask for row r-1: if incoming mask had 1 at column c, then (r-1, c) was already filled from above (i.e., it's the bottom of a vertical from row r-2). In that case, the cell (r-1, c) is not paired with any horizontal in that row because it's already occupied; it is part of a vertical domino from above. So we know its pairing: it is paired with (r-2, c). Similarly, if incoming mask had 0, then (r-1, c) was empty at start of row r-1; during processing we either placed a horizontal domino covering it and its neighbor, or we started a vertical domino from that cell (making it a top) which will cause the outgoing mask bit to be 1 for column c. So the pairing for cells in row r-1 can be determined from the combination of: incoming mask bits, and the decisions made while placing row r-1. When we aggregate states, we need to remember enough to later check blocks.
One approach: Do DP row by row where the state includes not only the occupancy mask for the next row, but also some pattern of horizontal domino placements in the current row. However, that could increase the state size. Since W ≤ 20, we could potentially keep track of the entire row's domino orientation: i.e., for each cell, whether it's covered by a horizontal domino (and if so, whether it's the left or right part) or vertical top or bottom. But vertical bottom from above is given by incoming mask; vertical top is indicated by outgoing mask. Horizontal dominoes are internal to the row and involve pairs.
Perhaps we can encode the row's domino configuration in a more compact way: For each column, we need to know if the cell is part of a horizontal domino (and which direction) or if it's a vertical domino (top or bottom). But the cross condition involves adjacency in both rows. It might be possible to check blocks on the fly during the row placement, column by column, using a small amount of state that remembers the left neighbor's status.
Another viewpoint: The tatami condition is equivalent to forbidding a certain local pattern. It might be possible to transform the problem into a perfect matching problem with additional constraints, or into counting of tilings of a grid with monomers and dimers? Not sure.
Since H,W ≤ 20, we could attempt a brute-force search with backtracking? 400 cells, each domino covers 2 cells, so 200 dominoes. The search space is huge; need smarter DP.
We can treat it as a constraint satisfaction on a grid graph. Use DP with state representing the current "profile" including connectivity information? The constraint is local (2x2). We could use transfer-matrix method with state that for each column, we store whether the cell in the current row is connected to the cell above (i.e., vertical domino) or not, and also for the previous row's horizontal connections maybe?
Idea: Process the grid row by row, and when we finish a row, we need to ensure that for each 2x2 block spanning the previous row and current row, the condition holds. That condition can be expressed in terms of the domino assignments of the four cells. The assignments for a cell can be categorized as:
- Type V_top: cell is the top of a vertical domino (paired with cell below).
- Type V_bottom: cell is the bottom of a vertical domino (paired with cell above). This is determined by the incoming mask.
- Type H_left: cell is the left part of a horizontal domino (paired with cell to right).
- Type H_right: cell is the right part of a horizontal domino (paired with cell to left).
But note that V_bottom cells are already occupied from above; they are not placed in the current row but are part of the row's configuration from previous decisions. When we process a row, we place the empty cells (those not occupied from above). The placed cells could be either H_left (if we place a horizontal domino covering this and the next cell) or V_top (if we start a vertical domino, which will occupy the cell below in next row). So after we finish a row, the row's configuration is fully determined: each cell is either V_bottom (if incoming mask bit=1) or H_left/H_right (if placed horizontally) or V_top (if we started a vertical). Actually if we start a vertical, the current cell becomes V_top, and we set the outgoing mask bit to 1.
Thus, for a given row, given the incoming mask (which tells which cells are V_bottom), and our placement decisions (which we can represent as a bitmask for outgoing mask plus maybe a pattern of horizontal dominoes), we can determine the type of each cell.
Then, after we have completed row r, we can check all 2x2 blocks between row r-1 and r. But to do that, we need to know the types of cells in row r-1. That information was part of the state from the previous row. So if we keep as state the entire row configuration of row r-1 (i.e., for each column, the type: V_bottom? Actually row r-1's types are known at the time we processed it. When we move to row r, we need to remember row r-1's configuration to later check blocks with row r. But after we finish row r, we then need row r's configuration to check with row r+1, etc. So we need to carry forward the configuration of the last row as part of the state.
Thus, DP state can be: the configuration of the previous row, plus perhaps the incoming mask for the current row? But the incoming mask for current row is exactly the outgoing mask from previous row, which is determined by the previous row's configuration (since outgoing mask bits are 1 for cells that are V_top). So if we store the previous row's configuration, we automatically know the incoming mask for the current row (which cells are V_bottom). However, the previous row's configuration also includes which cells are H_left/H_right or V_bottom, but V_bottom are already accounted for by incoming mask? Actually V_bottom cells in row r are those that were V_top in row r-1. So if we store the full configuration of row r-1, we can deduce which cells in row r are occupied from above: those columns where row r-1 had V_top. So we don't need a separate mask; the state can be the types of the previous row. Then when processing the current row, we need to place dominoes covering cells that are not already occupied by V_bottom (i.e., cells where row r-1 had V_top). That is analogous to having a mask of occupied cells.
So DP can go row by row, where state is the configuration of the previous row (row index i-1). For each state, we consider all ways to tile row i (given the occupied cells from vertical dominoes from above) such that the row i is fully covered, and such that after tiling row i, all 2x2 blocks between row i-1 and i satisfy the no-cross condition. Then we transition to a new state which is the configuration of row i (which will become the previous row for next row). At the end, after processing all rows, we need to ensure the final row has no vertical tops? Actually all cells must be covered. In the last row, we cannot start vertical dominoes because they would extend beyond the board. So in the final row, we must not have any V_top (i.e., outgoing mask must be 0). So the configuration for the last row must have no V_top; all cells must be either V_bottom (if above had vertical) or H_left/H_right (horizontal). But V_bottom in last row are allowed because they are the bottom of vertical dominoes started in previous row. So final state must be a configuration where no cell is V_top. That means all cells are either H_left/H_right or V_bottom.
Now, what is the representation of a row configuration? For each column j from 0 to W-1, we need to know the type. There are four possible types? Actually:
-
V_bottom: cell is occupied by a vertical domino from above (i.e., it's the lower cell of a vertical domino). In terms of the row's own generation, this cell is already filled and cannot be used for placement. It is not part of any horizontal or new vertical domino in this row. Its type is essentially "covered from above".
-
V_top: cell is the top of a vertical domino that extends downward. This cell is placed in this row (so it was empty initially) and we decided to start a vertical domino, so it will cause the cell below to be occupied (V_bottom) in the next row.
-
H_pair: For horizontal domino, we have two adjacent cells; one is left, one is right. So we can denote for a cell whether it is the left part of a horizontal domino (H_left) or right part (H_right). But both are essentially covered by a horizontal domino within the row. However, to ensure consistency (no overlapping), we need to know the pairing. A common way to represent a row's horizontal domino placement is by a bitmask indicating which cells are the left cell of a horizontal domino (i.e., a domino covering columns j and j+1). Alternatively, we can represent the row configuration by an array of "occupied" status plus pairing info.
But for the cross condition, we need to know, for a cell in row r-1, whether it is part of a horizontal domino (and thus its right neighbor is the same domino) or vertical (top or bottom). The cross condition involves edges: For a 2x2 block, we need to know if A and B are in same domino (i.e., horizontally paired in row r-1), if C and D are in same domino (horizontal pairing in row r), if A and C are in same domino (vertical pairing across rows), if B and D are in same domino (vertical pairing). So we need to know for each cell its partner (if any) in the left-right direction (horizontal) and up-down direction (vertical). The vertical pairing is determined by whether the cell is V_top (then partner is below) or V_bottom (partner is above). For a cell that is V_bottom, its partner is above (row r-1). For a cell that is V_top, its partner is below (row r+1). For a cell that is part of a horizontal domino, its partner is either left or right.
Thus, to check the 2x2 block between rows r-1 and r, we need for each of the four cells:
- For A (r-1,c): we need to know if it is horizontally paired with B (i.e., is it H_left and B is H_right, and they form a domino). Similarly, we need to know if it is vertically paired with C (i.e., if A is V_top and C is V_bottom). Actually if A is V_top, then it is paired with C only if C is V_bottom (which will be true if the vertical domino continues). But A being V_top means it started a vertical domino that goes down; then C must be V_bottom (occupied from above) as part of that domino. So the pairing is implicit if both rows are consistent. In our DP, when we place row r, we ensure that cells that are V_bottom in row r correspond to V_top in row r-1. So if we know the configuration of row r-1 and row r, we can deduce the vertical pairings: for column c, if row r-1 cell is V_top, then row r cell must be V_bottom (since that's the only way that vertical domino can be satisfied). In our transition, we will enforce that: when we have a V_top in row r-1, the corresponding cell in row r is already occupied (V_bottom) and we cannot place anything else there. So that condition is automatically enforced. Similarly, if row r-1 cell is V_bottom (meaning it was filled from above), then it is paired with row r-2, not with row r. So for the block between r-1 and r, only vertical pairings where row r-1 cell is V_top and row r cell is V_bottom are relevant. If row r-1 cell is V_bottom, then A is paired with above, so A and C are not in the same domino. So that edge would be a border unless A and C happen to be in same domino via some other? No, vertical dominoes only pair cells in adjacent rows. So if A is V_bottom, it's already paired with above, so it cannot also be paired with C. So A and C are different dominoes, meaning the vertical edge between them is a border (unless they are the same via some other mechanism? They can't be same). So for the vertical edge between A and C, it is a border unless A is V_top and C is V_bottom (i.e., they form a vertical domino). Similarly for B and D.
Thus, to evaluate the cross condition, we need to know for each column whether row r-1 cell is V_top (paired down) and row r cell is V_bottom (occupied from above). That's exactly determined by the configurations: if row r-1 config has V_top at column c, then row r config must have V_bottom at column c; otherwise, if row r-1 has anything else (V_bottom,