Plugged In

Time limit1sMemory limit128 MB

Summary
Given pin-to-hole connections on an n x n grid, find the socket orientation (rotations and flips) minimizing the average Manhattan wire length.
Level

Medium5 of 10

Topics
Implementation, Math, Brute force
Solved
No attempts yet

Problem

The designers of a new game console want to accept interactive input from many different sources. Using a special sensor-lined "electronic cocoon", a user could control simulated laser cannons by moving their eyebrows, accelerate or decelerate by wiggling their ears, steer in three-dimensional space by rotating their ankles, and so on -- the possibilities are endless.

The connection between the cocoon's sensors and the simulated actions is made through a special square plug with n2n^2 pins (the value of nn is not yet fixed). Each pin can carry the output of one sensor, though for some applications not every pin is active. The plug fits into a square socket with n2n^2 holes attached to the game inputs; again, not every hole is always used. The socket may be flipped over and rotated to produce different matchings between pins and holes. Pins and holes are each numbered consecutively in row-major order (for example, from 11 to n2n^2 for the plug and from 1' to n²' for the socket).

Because of these rotations and flips, when n=4n = 4 pin 1 can be aligned only with hole 1', 4', 13', or 16' (depending on how the plug is rotated relative to the socket). When all rotations and both the front and back of the plug are considered, pin 2 can be aligned only with hole 2', 3', 5', 8', 9', 12', 14', or 15'.

Most games need extra wiring, because a pin usually cannot be aligned directly over the hole it must connect to (for instance, connecting pin 1 to hole 11'). This wiring is done by a game-specific "wiring block" placed between the plug and the socket. The length of each wire depends on the orientation of the socket relative to the plug. Wires always run parallel to the grid lines, so the amount of wire between a pin and a hole is 11 plus the length of a shortest grid path between them (the extra 11 accounts for the thickness of the wiring block). Thus one unit of wire is the minimum possible, achieved when a pin sits directly over the hole it connects to.

Given a list of required connections, determine the minimum average wire length achievable over all orientations of the socket. The socket may be placed in any of its 88 orientations: the 44 rotations of the front face plus the 44 rotations of the flipped back face.

For example, for the connections (1, 3'), (5, 7'), (2, 6') on a 4×44 \times 4 plug and socket, the average wire length is 2.66672.6667 if the plug is inserted into the front of the socket without rotating it, but only 2.33332.3333 in the best orientation.

Input

The input consists of several scenarios. Each scenario begins with a positive integer nn (n≤100n \le 100), the side length of the plug and socket, on a line by itself, followed by a positive integer mm (m≤n2m \le n^2) on a line by itself, followed by mm lines. Each of these lines contains two positive integers in the range 1,…,n21, \dots, n^2: the first is a pin position in the plug and the second is a hole position in the socket. No two pairs share a common first element, and no two pairs share a common second element. The final scenario is followed by a line containing a single 00.

Output

For each scenario, output its number (starting from 11) together with the smallest average wire length achievable over all rotations and flips of the socket, on a line of the form:

Scenario n: smallest average = avg

where avg is the average, rounded and displayed to exactly four decimal places. Separate the output of consecutive scenarios with a single blank line.

Examples3

  1. Example 1

    Input
    4
    3
    1 3
    5 7
    2 6
    2
    3
    1 4
    2 2
    4 1
    0
    
    Expected output
    Scenario 1: smallest average = 2.3333
    
    Scenario 2: smallest average = 1.0000
    
  2. Example 2

    Input
    1
    1
    1 1
    0
    
    Expected output
    Scenario 1: smallest average = 1.0000
    
  3. Example 3

    Input
    2
    1
    1 4
    0
    
    Expected output
    Scenario 1: smallest average = 1.0000