#P7087. Sudoku Puzzle Similarity

    ID: 20293 Type: Default 1000ms 256MiB

Sudoku Puzzle Similarity

Sudoku Puzzle Similarity

Erast Kopi is a famous Sudoku puzzle designer. Due to many imitations and plagiarisms of his puzzles, he wants to collect evidence by checking if two puzzles are similar. A Sudoku puzzle is a 9×9 grid divided into nine 3×3 subgrids. In a valid filled sudoku puzzle every row, column, and 3×3 subgrid contains each of the digits from \(1\) to \(9\) exactly once. (Empty cells are allowed in a puzzle and are denoted by a dot ..)

Two puzzles \(P\) and \(Q\) are defined to be similar if one can transform \(P\) into \(Q\) by a sequence of the following operations:

  • Choose two digits \(x\) and \(y\) and swap all occurrences of \(x\) with \(y\) (i.e. apply a digit permutation on the filled cells).
  • Swap two row blocks (the groups of rows \(1\text{–}3\), \(4\text{–}6\), \(7\text{–}9\)).
  • Swap two rows within any row block.
  • Swap two column blocks (the groups of columns \(1\text{–}3\), \(4\text{–}6\), \(7\text{–}9\)).
  • Swap two columns within any column block.
  • Flip along the top‐left to bottom‐right diagonal (transpose the grid).

Your task is to determine if two given Sudoku puzzles are similar, i.e. if one can be transformed into the other by a series of the above operations. Note that these operations will not change the positions of the empty cells. Moreover, for any filled cell the relation of equality is preserved under any valid digit permutation. Thus, in order for two puzzles to be similar, the pattern of filled cells and the equality relations among the filled digits must be the same after applying an allowed geometric transformation.

inputFormat

The input consists of 18 lines. The first 9 lines describe the first Sudoku puzzle \(P\) and the next 9 lines describe the second Sudoku puzzle \(Q\). Each line contains exactly 9 characters. A character is either a digit from 1 to 9 (representing a filled cell) or a dot . (representing an empty cell).

outputFormat

Output a single line containing Yes if puzzle \(P\) is similar to puzzle \(Q\) under the allowed operations, and No otherwise.

sample

123456789
456789123
789123456
214365897
365897214
897214365
531642978
642978531
978531642
123456789
456789123
789123456
214365897
365897214
897214365
531642978
642978531
978531642
Yes