Knight's Journey(DFS练习)

Description

《Knight's Journey(DFS练习)》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

题目大意:给出棋盘大小p.q,判断马能否一次走完棋盘,走法按照国际象棋的规则。特殊要求,若能一次走完,按照字典序输出走的路径。棋盘坐标列为ABCD……行为1234…….,输出坐标字母在前,输出路径按照字典序。

思路:刚开始不知道国际象棋马的走法也不知道为什么输出保证字典序为啥就纵坐标在前横坐标在后,然后就不知道怎样搜才能保证搜出来的结果是保证字典序的。由大佬指点一番后,才真正理解到题意,棋盘坐标为:

 

 ABCDE
1 第三B1 第五D1 
2优先级第一A2   第七E2
3  马位置C3  
4第二A4   第八E4
5 第四B5 第六D5 

因为搜出的答案一定是全图只走一次,又要保证字典序,所以搜索的起始位置不影响结果(管你从哪儿起步字典序排出来第一个点都是最左上角的点)。在保证纵坐标(字母)的字典序条件下按行的数字顺序走。(固定了八个方向的走法顺序)这样走出来就能够保证搜出来的路径一定保证字典序。至于横纵坐标换位输出,应该是字典序排序原因(样例都换了那就跟着换……)。

上表解释:解法应该从最左上角开始搜索,所以起始点为A1,第二个点就是B3。对应到该表中就是C3->D5.

 

 

代码如下:

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<string>
#include<queue>
#include<set>
#include<map>
#include<math.h>
using namespace std;
int f[8][2]={{-1,-2},{1,-2},{-2,-1},{2,-1},{-2,1},{2,1},{-1,2},{1,2}};//固定顺序
int vis[27][27];//标记是否访问
int a,b,fag;
struct fun{
	int x;
	int y;
}s[30];//存坐标
void dfs(int x,int y,int k)//k为第几步
{
	if(fag==1) return ;//已找到
	s[k].x=x;s[k].y=y;
	if(k==a*b)//走完棋盘
	{
		for(int i=1;i<=k;i++)
		{
			printf("%c%d",s[i].y-1+'A',s[i].x);//输出
		}
		printf("\n");
		fag=1;//已找到路径,后面就不用搜了
	}
	for(int i=0;i<8;i++)//八个方向走
	{
		fun q;
		q.x=s[k].x+f[i][0];
		q.y=s[k].y+f[i][1];
		if(vis[q.x][q.y]==0&&q.x>0&&q.x<=a&&q.y>0&&q.y<=b)
		{
			vis[q.x][q.y]=1;
			dfs(q.x,q.y,k+1);
			vis[q.x][q.y]=0;//该走法不满足时需要访问归零回到原来位置重新走
		} 
	}
}
int main()
{
	int t,i,j;
	scanf("%d",&t);
	for(i=1;i<=t;i++)
	{
		memset(vis,0,sizeof(vis));
		fag=0;
		scanf("%d%d",&a,&b);
	    printf("Scenario #%d:\n",i);
		vis[1][1]=1;	
		dfs(1,1,1);//从最左上角A开始搜
		if(fag==0) printf("impossible\n");
		if(i!=t) printf("\n");
	}
	return 0;
}

 

 

 

    原文作者:骑士周游问题
    原文地址: https://blog.csdn.net/pleasantly1/article/details/81021804
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞