Doubly-sorted grid (small)
Time limit5sMemory limit512 MB
Given a partially filled R by C letter grid with R and C at most 4, count the completions whose rows and columns are non-decreasing modulo 10007.
- Level
Medium5 of 10
- Topics
- Backtracking, Dynamic programming
- Solved
- No attempts yet
Problem
A rectangular grid holds one lower case English letter in each cell. The grid is doubly sorted if the letters in every row are non-decreasing from left to right, and the letters in every column are non-decreasing from top to bottom. Of the four grids below, the first two are doubly sorted and the other two are not.
abc ace aceg base
def ade cdef base
ghi bdg xxyy base
You are given a grid in which only some of the cells are filled. Count the ways to fill the remaining cells so that the whole grid is doubly sorted. The answer can be large, so print the number of ways modulo .
Input
The first line contains the number of test cases . Then test cases follow. The first line of each test case contains the number of rows and the number of columns , separated by a space. The next lines each contain a string of length , and those strings describe the partially filled grid. Each character of the grid is either a lower case English letter or ., which marks a cell that is not filled yet.
Limits
- Each character of the grid is
.or a lower case English letter.
Output
For each test case, print one line in the format Case #X: y, where is the test case number starting from 1 and is the number of doubly sorted grids you can obtain, modulo .