QOJ.ac

QOJ

Time Limit: 9 s - 15 s Memory Limit: 1024 MB Total points: 30

#5810. Doubly-sorted Grid

Statistics

Problem

A rectangular grid with lower case English letters in each cell is called doubly sorted if in each row the letters are non-decreasing from the left to the right, and in each column the letters are non-decreasing from the top to the bottom. In the following examples, the first two grids are doubly sorted, while the other two are not:

abc    ace    aceg    base
def    ade    cdef    base
ghi    bdg    xxyy    base

You are given a partially-filled grid, where some of the cells are filled with letters. Your task is to compute the number of ways you can fill the rest of the cells so that the resulting grid is doubly sorted. The answer might be a big number; you need to output the number of ways modulo 10007.

Input

The first line of input gives the number of test cases, T. T test cases follow. Each test case starts with a line containing two integers R and C, the number of rows and the number of columns respectively. This is followed by R lines, each containing a string of length C, giving the partially-filled grid. Each character in the grid is either a lower-case English letter, or '.', indicating that the cell is not filled yet.

Output

For each test case, output one line. That line should contain "Case #X: y", where X is the case number starting with 1, and y is the number of possible doubly-sorted grids, modulo 10007.

Limits

Memory limit: 1 GB.

1 ≤ T ≤ 40

Each character in the partially-filled grid is either '.' or a lower-case English letter.

Small dataset (10 Points)

Time limit: 60 9 seconds.

1 ≤ R, C ≤ 4

Large dataset (20 Points)

Time limit: 120 15 seconds.

1 ≤ R, C ≤ 10

Sample

3
2 2
ad
c.
3 3
.a.
a.z
.z.
4 4
....
.g..
.cj.
....
Case #1: 23
Case #2: 7569
Case #3: 0
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.