TopCoder problem "LatticeCrossword" used in TCHS07 Beta 2 (Division I Level Three)



Problem Statement

    

A lattice crossword is a variant of a crossword where exactly 4 words are arranged in the form of a lattice (like a '#' sign). Given 4 words, a, b, c and d, return the number of different lattice crosswords that can be created according to the following rules:

  1. Each of the four given words must appear in the crossword exactly once.
  2. Two of the four words must be printed horizontally, from left to right.
  3. Two of the four words must be printed vertically, from top to bottom.
  4. Each horizontal word must cross both vertical words (each through exactly one common letter).
  5. Each vertical word must cross both horizontal words (each through exactly one letter).
  6. The horizontal words must be separated by at least one row.
  7. The vertical words must be separated by at least one column.

Two lattice crosswords are considered different if at least one of the words is at a different position. The position of a word is determined by the row and column position of its first letter. Row 0 is the row of the topmost letter in the crossword, and column 0 is the column of the leftmost letter in the crossword.

      c
      o
    p n
    r t
topcoder
    b s
  solution
    e
    m

Here the leftmost letter is "t" of the "topcoder" and the topmost letter is "c" of the "contest". So, the positions of the words are:

"topcoder" - (0, 4)

"contest" - (6, 0)

"problem" - (4, 2)

"solution" - (2, 6)

Another one lattice crossword, which can be constructed using this words is:

    t
    o
    p s
    c o
  problem
    d u
contest
    e i
      o
      n
 

Definition

    
Class:LatticeCrossword
Method:crossword
Parameters:String, String, String, String
Returns:int
Method signature:int crossword(String a, String b, String c, String d)
(be sure your method is public)
    
 

Constraints

-a, b, c and d will each contain between 3 and 15 characters, inclusive.
-a, b, c and d will be distinct.
-a, b, c and d will contain only lowercase letters ('a'-'z').
 

Examples

0)
    
"topcoder"
"contest"
"problem"
"solution"
Returns: 2
Example from the problem statement.
1)
    
"coder"
"number"
"rober"
"joker"
Returns: 0
No lattice crossword can be constructed.
2)
    
"lattice"
"crossword"
"disticnt"
"approach"
Returns: 4
3)
    
"zaxb"
"axc"
"cxd"
"bxdy"
Returns: 2
Two lattice crosswords:
           z
zaxb       axc
 x x  and  x x
 cxd       bxdy
   y
4)
    
"aaaaaaaaaaaaaaa"
"aaaaaaaaaaaaba"
"aaaaaaaaaaaaaab"
"baaaaaaaaaaaaaa"
Returns: 10082176
A lot of crosswords.

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=6532

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=10715&pm=6532

Writer:

gevak

Testers:

PabloGilberto , brett1479 , Olexiy

Problem categories:

Brute Force