Towers of Hanoi Grid
Time limit1sMemory limit1024 MB
Move d disks from the top-left to the bottom-right corner of an n by n grid, stepping only right or down, and report the minimum moves or impossible.
Problem
Towers of Hanoi is a rather famous problem for computer scientists as it is an excellent exercise in recursion. For those unfamiliar, here is the classic problem. You are given three pegs. On the first peg, there are disks stacked in decreasing order of size. The objective of the game is to move the entire tower from the first peg to the last peg. In each move, you may only move a single disk from the top of one stack to another stack. For the entire game, no disk of larger size is ever allowed to be placed on top of a disk of smaller size. The goal of the puzzle is to move the tower in the minimum number of moves.

In this problem, the pegs form an grid. The rows are numbered top to bottom from to , and the columns are numbered left to right from to . The original tower is placed on the top left peg . The goal is to move the tower to the bottom right peg in the minimum number of moves possible.

The game has some different but related rules:
- From peg , you may only move the top-most disk to peg or peg , and only if that peg exists.
- Only pegs and may have more than one disk at any time. All other pegs may hold at most one disk.
- You can choose any peg for each move.
- You still may never place a larger disk on a smaller disk.
Input
The first input line contains a positive integer , the number of grids to solve. The grids follow on the next lines, one grid per line. Each grid is described by two integers and (, ), the number of disks and the dimensions of the grid.
Output
For each grid, output Grid #d: v, where is the minimum number of moves to solve the Tower of Hanoi Grid puzzle. If it is not possible to move the disks from peg to peg , output impossible for . Leave a blank line after the output for each grid.