eolymp
bolt
Спробуйте наш новий інтерфейс для відправки розв'язків
Задачі

Рівносторонні доміно

Рівносторонні доміно

We've all seen regular dominoes before - they look like rectangular tiles, twice as long as they are wide, with pips designating a number between \textbf{1} and \textbf{6} on each of their two ends. The object of a dominoes game involves some variation of tiling the dominoes so that the numbers on adjacent dominoes match up. Now what if we were to make the dominoes out of equilateral triangles, like this: \includegraphics{https://static.e-olymp.com/content/bd/bda6ab2a458855aad6a676af33d07ecd959cf960.jpg} An equilateral domino is shaped like a quadrilateral made up of two equilateral triangles. Again, the triangle at each end depicts a number from \textbf{1} to \textbf{6}. Two equilateral dominoes can be tiled next to each other if the domino ends on either side of their shared edge have the same value, and if neither domino overlaps another. Once you have begun a tiling, additional dominoes may only placed adjacent to one or more of the dominoes already on the table. In other words, two more disjoint groups of dominoes does not constitute a valid tiling. For example, equilateral dominoes may be tiled like this: \includegraphics{https://static.e-olymp.com/content/37/37dbddabf137dd817c071d7988b162ba4d2cfd1b.jpg} Your task is to fi nd a best tiling, in some sense, of some equilateral dominoes. If you score one point for every edge that is shared between two dominoes, what is the best way to tile a given set of equilateral dominoes to achieve the highest score? \InputFile The input consists of multiple test cases. The first line of each test case contains an integer \textbf{N}, \textbf{1} ≤ \textbf{N} ≤ \textbf{6}, the number of equilateral dominoes in that set. This is followed by \textbf{N} lines with two integers each (values between \textbf{1} and \textbf{6} inclusive), with each line indicating the pip values on each of the dominoes in the set. Input is followed by a single line with \textbf{N = 0}, which should not be processed. \OutputFile For each test case, output a single line containing the highest tiling score that can be achieved with the given set of equilateral dominoes. If there is no valid tiling of the dominoes, output a zero.
Ліміт часу 1 секунда
Ліміт використання пам'яті 64 MiB
Вхідні дані #1
4
1 2
2 3
3 2
4 3
2
5 6
2 1
4
3 2
3 4
1 5
1 6
0
Вихідні дані #1
4
0
1