Hexagonal Game
Time Limit: 10 Seconds Memory Limit: 32768 KB
Hexagonal game is a single player game played on a chessboard like the following illustration. You are given two specified positions on the chessboard and several hexagonal cards at the starting of the game. Each card has six sides that each with a number on. The goal is to find a shortest path connecting the two positions and fill the path with cards. All the connecting sides of adjacent cards along the path should have the same number on. The bonus you get is the square of the number on the connecting sides. You are to find an arrangement to get the maximal bonus.
Note:
All the cards can be flipped and rotated before the arrangement.
There may be several shortest paths, any one will be ok, just to make the bonus maximal.
Input
There are several test cases. Each case started with two number, m and n, indicated the distance between the two specified positions by row and by column. (0<=m<=6, 0<=n<=3).
Then, n+m+1 lines followed to describe the cards, each line has six positive numbers in the ranger of [1, 6].
m = n = 0 indicate the end of input.
Output
One line contains the maximal bonus.
If the mission is impossible, output "mission impossible"
Sample Input
2 0 1 2 4 4 5 6 5 6 3 3 3 3 2 2 2 2 2 2 1 1 1 2 4 4 5 6 5 6 3 3 3 3 2 2 2 2 2 2 1 0 1 1 1 1 1 1 2 2 2 2 2 2 0 0
Sample Output
29 40 mission impossibleSubmit
Source: ZOJ Monthly, February 2005