14711 - 3D Bingo Cube   

Description

!! Challenging

You are given an n x n x n cube.
Each small cell of the cube contains a unique integer from 1 to n3

A sequence of n3 numbers will then be drawn one by one.
Whenever a number is drawn, it is marked in the cube.

If, at any point, all cells along a straight line are marked, we say “bingo.”
A straight line may be:

  • A row along the x-axis,

  • A column along the y-axis,

  • A line along the z-axis,

  • A diagonal on one of the cube’s faces,

  • Or one of the four main space diagonals connecting two opposite corners of the cube.

Your task is to determine which drawn number first causes a “bingo.”

Input

  • The first line contains an integer n (0 < n ≤ 10)

  • The following n3 integers describe the cube’s contents in row-major order.

    • That is, the numbers are listed layer by layer, row by row, and column by column.

  • The next n3 integers represent the sequence of numbers being drawn, in order.

    • For readability, the numbers may be given with n numbers per line.

Example Walkthrough (Sample Input 1)

3
2 18 4
3 1 20
14 6 12
7 21 15
6 9 17
19 5 22
8 23 12
1 3 5
7 9 11
13 15 17
19 21 23
25 27 2
4 6 8
10 12 14
16 18 20
22 24 26

  • First Line:
    The integer 3 tells us the cube size is 3×3×3.
    That means the cube contains 33=27 cells, each filled with a unique number from 1 to 27.

  • Next 27 Numbers (Cube Contents):
    The following numbers describe how the cube is filled.
    They are given in row-major order: layer by layer, row by row, left to right.

    For n=3, these 27 numbers form a 3×3×3 cube:

Layer 1 (z = 0)
1 18 4
13 2 20
24 10 26

 

Layer 2 (z = 1)
27 14 7
3 25 16
21 15 11

 

Layer 3 (z = 2)

6 9 17
19 5 22
8 23 12
  • Next 27 Numbers (Draw Sequence):

After the cube contents, the next block of numbers lists the draw order.

These are the numbers being drawn one by one, and whenever a number is drawn, the corresponding cell in the cube is marked.

The sequence here is:

1 3 5 7 9 11 13 15 17 19 21 23 25 27 2 4 6 8 10 12 14 16 18 20 22 24 26

 

Output

Print a single integer: the first drawn number that causes a bingo.

Cube contents (layer by layer, row-major):

For example,

Layer 1 (z = 0)
1 18 4
13 2 20
24 10 26

 

Layer 2 (z = 1)
27 14 7
3 25 16
21 15 11

 

Layer 3 (z = 2)

6 9 17
19 5 22
8 23 12
 

 

The sequence here is:

1 3 5 7 9 11 13 15 17 19 21 23 25 27 2 4 6 8 10 12 14 16 18 20 22 24 26

 

As we mark numbers in this order, the cube first achieves bingo when 19 is drawn.

 

Sample Input  Download

Sample Output  Download

Tags

11410EE 231002



Discuss