poj2488
A Knight's Journey
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18085 | Accepted: 6095 |
Description

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 is two squares in one direction and one square perpendicular to this. The world of a knight is the chessboard he is living on. Our knight lives on a chessboard that has a smaller area than a regular 8 * 8 board, but it is still rectangular. Can you help this 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 in the first line. The following lines contain n test cases. Each test case consists of a single line with two 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 how many different square letters exist. These are the first q letters of the Latin alphabet: A, . . .
Output
The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the lexicographically first path that visits all squares of the chessboard with knight moves followed by an empty line. The path should be given on a single 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 exist, you should output impossible on a single line.
If no such path exist, you should output impossible on a single line.
Sample Input
3 1 1 2 3 4 3
Sample Output
Scenario #1: A1 Scenario #2: impossible Scenario #3: A1B3C1A2B4C2A3B1C3A4B2C4
這個題目要求給出遍歷棋盤的字典序,所以要給擴展一個順序,我也不明白為什么這樣是字典序
dx[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
這個搜索順序總是先找字母數字小的,即以左上角為原點,當前為(i,j)總是找比先找比該點行或列小的,在找行或列大的,
行列中總是先找行
這里的行要按字母順序,列要按數字順序,樣例很糾結、
如果是存在能遍歷棋盤上所有點的解的話,那從棋盤上任意一點出發總能遍歷棋盤上所有的點,、
這里要1求字典序#include<stdio.h>
2#include<string.h>
3#include<math.h>
4int dx[8][2]={{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};
5int stack[100][2];
6int n,m,tot;
7int mark[27][27];
8short flag;
9void print()
10{
11int i;
12for (i=1;i<=tot ;i++ )
13{
14printf("%c%d",stack[i][0]+64,stack[i][1]);
15}
16printf("\n");
17}
18void dfs(int num)
19{
20int i,xn,yn,x,y,numn;
21if (num==tot&&!flag)
22{
23print();
24flag=1;
25return;
26}
27x=stack[num][0];
28y=stack[num][1];
29for (i=0;i<8 ;i++ )
30if (flag==0)
31{
32xn=x+dx[i][0];
33yn=y+dx[i][1];
34if ((xn>0)&&(xn<=n)&&(yn>0)&&(yn<=m)&&(mark[xn][yn]==0))
35{
36numn=num+1;
37mark[xn][yn]=1;
38stack[numn][0]=xn;stack[numn][1]=yn;
39dfs(numn);
40mark[xn][yn]=0;
41}
42}
43else return;
44}
45int main()
46{
47int t,i;
48scanf("%d",&t);
49for (i=1;i<=t ;i++ )
50{
51scanf("%d%d",&m,&n);
52memset(mark,0,sizeof(mark));
53memset(stack,0,sizeof(stack));
54mark[1][1]=1;
55tot=n*m;
56flag=0;
57stack[1][0]=1;
58stack[1][1]=1;
59printf("Scenario #%d:\n",i);
60dfs(1);
61if (!flag)
62{
63printf("impossible\n");
64}
65//if (i!=t)
66{
67printf("\n");
68}
69}
70return 0;
71}
72
所以應從(1,1)開始遍歷