poj 2488 DFS

A Knight's Journey

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 10   Accepted Submission(s) : 4
Problem Description
Background 
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.
 
Sample Input
3 1 1 2 3 4 3
 
Sample Output
Scenario #1:
A1
Scenario #2:
impossible
Scenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4
 

如图,对于骑士所在的位置,所标识的1 2 3 4...位置就是组成字典序的顺序。
View Code
 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 using namespace std;
 5 
 6 int vis[26][26],path[26][2];
 7 int flag,a,b;
 8 
 9 int dir[8][2]={-2,-1,-2,1,-1,-2,-1,2,    //这样子排数字是为了保证字典序
10                 1,-2, 1,2, 2,-1, 2,1};
11 
12 void dfs(int s,int t, int k)
13 {
14     int i,j;
15     if(k==a*b)
16     {
17         for(i=0;i<k;i++)
18             printf("%c%d",path[i][0]+'A',path[i][1]+1);
19         printf("\n");
20         flag=1;
21     }
22     else 
23     {
24         for(j=0;j<8;j++)
25         {
26             int n=s+dir[j][0];
27             int m=t+dir[j][1];
28             if(n>=0 && n<b && m>=0 && m<a && vis[n][m]==0 && !flag)
29             {
30                 vis[n][m]=1;
31                 path[k][0]=n;
32                 path[k][1]=m;
33                 dfs(n,m,k+1);
34                 vis[n][m]=0;
35             }
36         }
37     }
38 }
39 
40 int main()
41 {
42     int n,t;
43     scanf("%d",&n);
44     for(t=1;t<=n;t++)
45     {
46         flag=0;
47         int i,j;
48         scanf("%d%d",&a,&b);
49         for(i=0;i<a;i++)
50         {
51             for(j=0;j<b;j++)
52             {
53                 vis[i][j]=0;
54             }
55         }
56         vis[0][0]=1;
57         path[0][0]=0;path[0][1]=0;
58         printf("Scenario #%d:\n",t);
59         dfs(0,0,1);
60         if(flag==0) printf("impossible\n");
61         printf("\n");
62     }
63     //system("pause");
64     return 0;
65 }
原文地址:https://www.cnblogs.com/shenshuyang/p/2608999.html