A Knight ' s Journey

Source: Internet
Author: User

Description

Background
The knight is getting bored of seeing, the same black and white squares again and again and have decided to make a journey
Around the world. Whenever a knight moves, it is the squares in one direction and one square perpendicular to this. The world of a knight is the chessboard he was living on. Our knight lives in a chessboard that have a smaller area than a regular 8 * 8 board, but it's still rectangular. Can adventurous knight to make travel plans?

problem
Find a path such that the knight visits every square once. The knight can start and end on any square of the board.

Input

The input begins with a positive integer n on the first line. The following lines contain n test cases. Each of the test case consists of a single line with the positive integers p and q, such that 1 <= p * Q <= 26. This represents a p * q chessboard, where p describes how many different square numbers 1, ..., p exist, Q describes Ho W many different square letters exist. These is the first Q letters of the Latin alphabet:a, ...

Output

The output for every scenario begins with a line containing "scenario #i:", where I am the number of the scenario starting at 1. Then print a single line containing the lexicographically first path this visits all squares of the chessboard with Knight Moves followed by a empty line. The path should is given on a, the names of the visited squares by concatenating. Each square name is consists of a capital letter followed by a number.
If no such path exist, you should output impossible on a.

Sample Input

31 12 34 3

Sample Output

Scenario #1: A1scenario #2: Impossiblescenario #3: A1B3C1A2B4C2A3B1C3A4B2C4

Source

TUD Programming Contest 2005, DARMSTADT, Germany The main idea: a knight from the origin, only a cursory line, the need to traverse all the path of the dictionary ordering (that is, A and b then C), with the DFS thought to traverse each state, If the last step equals the size of the graph then the output is straightforward and difficult to sort in the dictionary order (although it is not yet known why the Dir is written in this array--| | ), use visit to record the points that have passed, and finally if the DFS fails then visit to be 0 again.
#include <cstdio>#include<cstring>using namespacestd;Const intINF = -;intVisit[inf][inf];intdir[8][2] = {{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};intflag, p, Q,ans;structedge{Chars; intnum;} A[inf*INF];voidDfsintXintYintStep) {    if(Flag = =0)return  ; if(Visit[x][y])return ; Visit[x][y]=1; if(Step = =ans) {Flag=0;  for(inti =1; I <= ans;i++) printf ("%c%d", A[i].s,a[i].num); printf ("\ n"); return ; }     for(inti =0; I <8; i++){        intX1 = x + dir[i][0]; intY1 = y +dir[i][1]; if(X1 <1|| Y1 <1|| X1 >p| | Y1 > Q| | VISIT[X1][Y1] = =1)            Continue; A[step+1].S ='A'+ Y1-1; A[step+1].num =X1; DFS (X1,y1,step+1); VISIT[X1][Y1]=0; }}intMain () {intT,count =1; scanf ("%d",&T); while(t--) {memset (visit,0,sizeof(visit)); Flag=1; a[1].S ='A', a[1].num =1; scanf ("%d%d",&p,&q); Ans= p*Q; printf ("Scenario #%d:\n", count++); DFS (1,1,1); if(Flag = =1) printf ("impossible\n"); if(t!=0) printf ("\ n");}return 0;}
View Code

A Knight ' s Journey

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.