D: Dominoes Magic Squares Source file name: dominoes.c, dominoes.cpp, dominoes.java, or dominoes.py Author: Rodrigo Cardoso A domino set is a collection of tiles of the form [a | b] with integer labels a and b satisfying 0 ≤ a, b ≤ 6. Both [a | b] and [b | a] are descriptions of the same domino tile. A complete domino set has exactly 28 tiles and the sum of all its labels is 168. A magic square is a square of integer numbers whose rows, columns, and diagonals have the same sum. Since domino tiles can be seen as planar objects of 2 unit squares, they can be used to build magic squares. For instance, the set of domino tiles [1 | 4] , [5 | 2] , [4 | 4] , [2 | 3] , [5 | 4] , [5 | 3] , [1 | 3] , [3 | 3] can be arranged into a magic square of side 4 units with rows, columns, and diagonals adding up to 13: However, it is impossible to build a 4 × 4 magic square with the following set of titles adding up to 15 in rows, columns, and diagonals: [6 | 5] , [2 | 4] , [2 | 2] , [5 | 5] , [5 | 4] , [5 | 1] , [2 | 3] , [3 | 6] . Assume you are given 8 domino tiles: can you arrange them into a 4 × 4 magic square? Input The input consists of several test cases. A test case comprises 8 consecutive lines of input, each one containing two blank-separated integers a and b, 0 ≤ a, b ≤ 6, representing the tile [a | b]. You can assume that a test case does not contain repeated dominoes. The input must be read from standard input. Output For each test case, output one line with the unique character ‘Y’ if a magic square can be built with the given domino tiles and ‘N’ otherwise. The output must be written to standard output.
2018 ACIS REDIS - XXXII Colombian Programming Contest - ACM ICPC 7 Sample Input 14 52 44 23 54 53 13 33 65 24 22 54 55 51 23 36 Sample Output Y N