Problem C
Connect the Dots
A famous logical problem is that of connecting 9 dots on a paper by drawing 4 line segments with a pencil, while never lifting the pencil from the paper. While this is easy enough (although it requires some thinking outside of the box), Simone has recently been building a game called “Connect the Dots” around a generalisation of the concept.
In Connect the Dots, you are presented with a
![\includegraphics[width=0.3\textwidth ]{sample-eps-converted-to.pdf}](/problems/connectdots/file/statement/en/img-0001.png)
Simone asked you to try the puzzle out, while betting you a balloon that it would be too hard. Prove her wrong by writing a program that solves the puzzle for you!
Input
The input consists of:
-
lines, each with integers, the numbers of the dots in the grid. The th number on the th line is the number of the th dot in the th row of the grid of dots.
The
Output
Output the minimum number of line segments needed to connect all the dots in order.
Sample Input 1 | Sample Output 1 |
---|---|
1 2 3 4 10 11 12 5 9 16 6 13 8 7 15 14 |
6 |
Sample Input 2 | Sample Output 2 |
---|---|
1 2 3 4 8 9 10 11 7 15 16 12 6 14 13 5 |
7 |