#P1556. [USACO12MAR] 幸福的路 Connect the Cows B

[USACO12MAR] 幸福的路 Connect the Cows B

Description

Every day, John bustles around to ensure the health and happiness of nn (1n101 \leq n \leq 10) cows.

Each cow’s position is given by a 2D coordinate, and John starts at the origin (0,0)(0, 0). To make the route more interesting, John decides to move only along directions parallel to the coordinate axes, that is, only north, south, east, or west. Moreover, John changes direction only upon reaching the coordinate of some cow (of course, if necessary, John may also pass through a cow’s coordinate without changing direction).

If John changes direction, he turns in place by 9090^\circ or 180180^\circ. John’s path must visit all cows and then return to the origin.

John may pass through any cow’s coordinate without changing direction any number of times. Please compute how many routes allow John to visit all nn cows, such that he changes direction exactly once at each cow’s coordinate. The same geometric path traversed in the opposite direction should be counted twice.

Input Format

The first line contains an integer nn, as described above.

The next nn lines each contain two space-separated integers xx and yy, where line i+1i+1 gives the coordinates of the ii-th cow (1000x,y1000-1000 \leq x, y \leq 1000).

Output Format

Output a single integer on one line, the number of valid routes (output 0 if there are no valid routes).

4
0 1
2 1
2 0
2 -5
2

Hint

Translated by ChatGPT 5