#P13412. [GCJ 2010 Finals] The Paths of Yin Yang

[GCJ 2010 Finals] The Paths of Yin Yang

题目背景

So, If and Else grow out of each other;
Hardness and Tractability complete each other;
Long int and Short int shape each other;
High bits and Low bits determine each other;
Music and Voice give harmony to each other;
Push_front and Push_back give sequence to each other.
-- Tao Te Ching, Laozi, Zhou dynasty, ancient China.
Translated (loosely) by yours truly.

题目描述

Given an rectangular grid of NN rows and MM columns, each cell can be labeled black (Yin) or white (Yang). Two cells are neighbors if they share a common unit-length edge segment. The grid is valid if all the black cells form a path, and all the white cells form a path. A path is a set ss of cells defined as follows:

  • The cells form a connected piece. From each cell in ss, you can reach any other cell in ss by moving between neighbors within ss.
  • Exactly two cells in ss have exactly one neighbor in ss each. These are the "ends" of the path.
  • Every other cell in ss has exactly two neighbors in ss.

For example, in the picture below, the first grid is valid, while the second grid is not -- although the black cells form a path, the white cells do not.

Given NN and MM, compute the number of valid grids. Note that symmetry doesn't matter -- as long as two valid grids differ in one position they are considered different, even if one can be rotated or flipped to the other.

输入格式

The first line of the input will be a single integer TT, the number of test cases. TT lines follow, each of which contains two integers separated by a space: "NN MM", as defined above.

输出格式

For each test case, output a line in the form "Case #xx: AA", where xx is the case number, starting from 1, and AA is the number of valid grids of the specified size.

3
4 4
4 6
5 5
Case #1: 24
Case #2: 44
Case #3: 48

提示

Limits

  • 1T501 \leq T \leq 50

Small dataset (Test set 1 - Visible)

  • Time limit: 30 15 seconds per test set.
  • 4N,M104 \leq N, M \leq 10

Large dataset (Test set 2 - Hidden)

  • Time limit: 120 60 seconds per test set.
  • For 80% of the test cases, 4N,M504 \leq N, M \leq 50
  • For 90% of the test cases, 4N,M704 \leq N, M \leq 70
  • For all test cases, 4N,M1004 \leq N, M \leq 100