The National Association of Museum Curators came to you with an interesting problem. The President of the country, in order to improve his public image, has decided to visit the various Art Museums in the country, to convey the impression that he is a refined man. Being a very busy person, however, and knowing nothing about art, he imposed two restrictions for the visits:
The curators are willing to follow the President’s demands, but they do not want to redistribute the masterpieces in the exposition only to obtain a straight path. Their only concession is to exchange temporarily the place of two masterpieces, if this helps to obtain a shorter path.
You should write a program that receives as input the layout of an exposition and finds the shortest path, according to the above constraints. To make your task easier, the curators have already defined a standard layout. Figure 7 shows one such layout.
| 10 | B | B | B | B | B | B | F | F | F | F | F |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 9 | A | A | A | A | A | B | D | C | C | F | F |
| 8 | A | F | F | F | A | B | A | A | C | F | C |
| 7 | B | F | E | F | A | B | B | B | B | B | D |
| 6 | F | F | D | E | A | B | A | A | A | B | A |
| 5 | E | E | D | E | E | E | E | E | A | B | B |
| 4 | D | D | D | E | E | E | E | E | A | A | B |
| 3 | D | C | C | F | F | F | C | C | A | B | A |
| 2 | D | C | C | F | F | F | C | C | A | A | A |
| 1 | C | C | C | C | C | C | C | C | C | C | C |
| Y/X | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
Figure 7: Layout of the Museum
The President’s walk begins always at the left wall (X = 1, any Y) and ends at the right wall (X = Xmax, any Y). The walk can be done horizontally or vertically; diagonal movements are not allowed. The works of a given artist are all labeled with the same capital letter (A, B, C, etc). From Figure 1, several cases can be illustrated:
The input file may contain several instances of the problem. Each instance has the following format (all numbers are positive integers):
A line containing two zeros terminates the input file. Numbers are separated by spaces.
For each instance of the problem, your program should produce output as follows.
If a path exists, your program should first print one line with the message “Exchange (x,y) and (u,v)” if an exchange will occur, or “No exchange” otherwise. Following that, the shortest path should be printed, one coordinate per line. In case more than one path is the shortest, any one of them may be printed, except that a path without an exchange should be preferred to those with exchanges.
If a path does not exist, your program should print only one line with the message “No path”.
The output of each instance is terminated with a blank line.