Under construction

Still being built. Things may change

Knight Tour

Interview

Time limit2sMemory limit128 MB

Summary
Check whether a given sequence of 36 squares on a 6x6 board forms a valid closed knight's tour with legal moves between consecutive squares and back to the start.
Level

Easy3 of 10

Topics
Simulation, Array, Hash map
Solved
No attempts yet

Problem

A knight's tour is a path on a chessboard in which a knight visits every square exactly once, and from the last square it can move back to the starting square with a legal knight move.

Each square of a 6×6 chessboard is written as one letter from A through F followed by one digit from 1 through 6. Given the 36 squares visited by a knight in order, determine whether the path is a valid knight's tour.

Input

The input consists of 36 lines, each giving one square in the order visited by the knight. Every square in the input exists on the 6×6 chessboard.

Output

Print Valid if the path is a valid knight's tour. Otherwise, print Invalid.

Examples5

  1. Example 1

    Input
    A1
    B3
    A5
    C6
    E5
    F3
    D2
    F1
    E3
    F5
    D4
    B5
    A3
    B1
    C3
    A2
    C1
    E2
    F4
    E6
    C5
    A6
    B4
    D5
    F6
    E4
    D6
    C4
    B6
    A4
    B2
    D1
    F2
    D3
    E1
    C2
    
    Expected output
    Valid
    
  2. Example 2

    Input
    A1
    C2
    E3
    F5
    D4
    B3
    A1
    C2
    E3
    F5
    D4
    B3
    A1
    C2
    E3
    F5
    D4
    B3
    A1
    C2
    E3
    F5
    D4
    B3
    A1
    C2
    E3
    F5
    D4
    B3
    A1
    C2
    E3
    F5
    D4
    B3
    
    Expected output
    Invalid
    
  3. Example 3

    Input
    D4
    F5
    D6
    B5
    A3
    B1
    D2
    F1
    E3
    D1
    F2
    E4
    F6
    D5
    B6
    A4
    B2
    C4
    A5
    C6
    E5
    F3
    E1
    C2
    A1
    B3
    C5
    E6
    F4
    E2
    C3
    A2
    C1
    D3
    B4
    A6
    
    Expected output
    Invalid
    
  4. Example 4

    Input
    D4
    F5
    D6
    B5
    A3
    B1
    D2
    F1
    E3
    D1
    F2
    E4
    F6
    D5
    B6
    A4
    B2
    C4
    A5
    C6
    E5
    F3
    E1
    C2
    A1
    B3
    C5
    A6
    B4
    A2
    C3
    E2
    C1
    D3
    F4
    E6
    
    Expected output
    Valid
    
  5. Example 5

    Input
    C5
    D3
    F2
    D1
    B2
    A4
    B6
    D5
    C3
    E4
    F6
    B3
    A1
    C2
    E1
    F3
    E5
    C6
    A5
    C4
    A3
    B1
    D2
    F1
    E3
    F5
    D6
    B5
    D4
    E6
    F4
    E2
    C1
    A2
    B4
    A6
    
    Expected output
    Invalid