#P10551. Grid Connection Assignment Counting
Grid Connection Assignment Counting
Grid Connection Assignment Counting
You are given an \(N\times M\) grid. Your task is to count the number of ways to fill every cell of the grid with one of the numbers \(1\), \(2\), or \(3\) such that there exists at least one way to choose a set of connections between cells sharing a common edge that satisfies the following conditions:
- For every cell with value \(1\) or \(3\), if a neighbor (up, down, left or right) is filled with \(2\), then that cell must be connected (in the chosen connection scheme) to that neighbor. (In other words, every cell with value \(1\) or \(3\) must have at least one adjacent cell with value \(2\).)
- For every cell with value \(2\), it must be connected to at least one adjacent cell with value \(1\) and at least one adjacent cell with value \(3\) (i.e. it must have at least one neighbor equal to \(1\) and at least one neighbor equal to \(3\)).
Note that a cell is considered adjacent to another if they share a common edge. The connection scheme is not unique – you only need the existence of at least one valid way to make the connections according to the above rules. It turns out that such a connection scheme exists if and only if the filled grid satisfies the following local conditions:
- If a cell is filled with \(1\) or \(3\), then it must have at least one neighbor with value \(2\).
- If a cell is filled with \(2\), then it must have at least one neighbor with value \(1\) and at least one neighbor with value \(3\).
Your task is to compute the total number of valid assignments.
inputFormat
The input consists of a single line containing two space‐separated integers \(N\) and \(M\), representing the number of rows and columns of the grid respectively.
outputFormat
Output a single integer — the number of valid assignments that satisfy the conditions.
sample
1 1
0