Loading

POJ 2488 A Knight's Journey

转载自:https://blog.csdn.net/riba2534/article/details/54176523  

题目:

 

A Knight's Journey
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 53183   Accepted: 18072

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,1),所以可以直接从(1,1)开始dfs
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <stack>
 5 #include <queue>
 6 #include <vector>
 7 #include <cmath>
 8 #include <algorithm>
 9 #define mem(a,b) memset(a,b,sizeof(a))
10 using namespace std;
11 int go[8][2]= {{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};//字典序方向
12 int vis[30][30];
13 int n,m,flag;
14 struct node
15 {
16     int x,y;
17 } a[30]; //存储每一步的坐标
18 void dfs(int x,int y,int step)
19 {
20     a[step].x=x,a[step].y=y;//把当前路径存入结构体
21     if(step==n*m)//搜完每一个格子打印路径
22     {
23         for(int i=1; i<=step; i++)
24             printf("%c%d",a[i].y-1+'A',a[i].x);//打印路径
25         printf("\n");
26         flag=1;
27     }
28     if(flag)return;
29     for(int i=0; i<8; i++)
30     {
31         int xx=x+go[i][0];
32         int yy=y+go[i][1];
33         if(xx>0&&xx<=n&&yy>0&&yy<=m&&vis[xx][yy]==0)//判断是否越界
34         {
35             vis[xx][yy]=1;//搜过的标记
36             dfs(xx,yy,step+1);
37             vis[xx][yy]=0;//标记回来
38         }
39     }
40 }
41 int main()
42 {
43     int t,ci=1;
44     scanf("%d",&t);
45     while(t--)
46     {
47         mem(vis,0);
48         flag=0;
49         scanf("%d%d",&n,&m);
50         printf("Scenario #%d:\n",ci++);
51         vis[1][1]=1;//先把第一个坐标标记了
52         dfs(1,1,1);//从(1,1)开始搜索
53         if(flag==0)printf("impossible\n");
54         printf("\n");
55     }
56     return 0;
57 }

 




posted @ 2019-02-16 15:04  拾月凄辰  阅读(294)  评论(0编辑  收藏  举报