JOI Flag
Time limit5sMemory limit128 MB
Count fillings of an M by N grid with J, O, I (some cells fixed) that contain at least one L shape: J with O to its right and I below, modulo 100000.
- Level
Medium5 of 10
- Topics
- Dynamic programming, Combinatorics, Implementation, Matrix
- Solved
- No attempts yet
Problem
A committee designed a flag based on the JOI logo. The flag is an grid, and each cell holds exactly one of the three letters J, O, I.
A flag is called good if the following L-shaped arrangement appears at least once: some cell contains J, the cell immediately to its right contains O, and the cell immediately below the J contains I.
J O
I .
(The . cell may hold any letter; only the three marked cells matter.) Formally, the flag is good if there exist a row and a column with and such that the cell at row , column is J, the cell at row , column is O, and the cell at row , column is I.
Some cells are already fixed to a specific letter. The remaining cells are still undecided and may become any of J, O, I. Given , , and which cells are fixed, count the number of ways to fill in the undecided cells so that the resulting flag is good. Print this count modulo .
Input
The first line contains two integers and (), the number of rows and columns of the flag.
Each of the next lines contains characters describing one row of the flag. Each character is J, O, I, or ?. A letter means the cell is already fixed to that letter; ? means the cell is still undecided.
Output
Print a single integer: the number of good flags, modulo .
Hint
Only the three cells of the L shape are constrained; the cell diagonally opposite the J (the bottom-right of the block) may hold any letter. A single occurrence anywhere in the flag is enough to make it good, so it is often easier to count the flags that are not good and subtract from the total number of fillings.