A Knight's Journey
Time limit1sMemory limit128 MB
Find the lexicographically smallest knight's tour that visits every square of a rectangular board with at most 26 squares exactly once.
- Level
Medium6 of 10
- Topics
- Backtracking, DFS, Brute force, Implementation
- Solved
- No attempts yet
Problem
The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey around the world. Whenever a knight moves, it moves two squares in one direction and one square perpendicular to that direction.
The world of a knight is the chessboard he lives on. Our knight lives on a chessboard that is smaller in area than a regular board, but is still rectangular. Can you help this adventurous knight make its travel plans?
The eight possible moves of a knight.
Find a path in which the knight visits every square exactly once. The knight may start and end on any square of the board.
Input
The first line contains a positive integer . The following lines contain test cases.
Each test case consists of a single line with two positive integers and such that . This represents a chessboard, where is the number of distinct square numbers and is the number of distinct square letters. The letters are the first letters of the Latin alphabet:
Output
For every scenario, first print a line containing "Scenario #i:", where is the scenario number, starting at 1. Then print, on a single line, the lexicographically first path that visits all squares of the chessboard using knight moves, followed by an empty line. The path is written on one line by concatenating the names of the visited squares. Each square name consists of a capital letter followed by a number.
If no such path exists, print impossible on a single line.