这题其实只有一点需要注意,就是国际象棋坐标系的选取。
国际象棋有个特点,标号的时候行为数字1~8,列为字母A~H,行从上到下为纵方向,列从左到右为横方向。不同于一般的坐标系,上个图就明白了:

剩下的就是普通的深搜了。
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <climits>
using namespace std;
const int MAXN = 10;
const int INF = INT_MAX;
int p, q;
bool visit[MAXN][MAXN];
int direction[8][2] = {
{-1, -2}, {1, -2}, {-2, -1}, {2, -1}, {-2, 1}, {2, 1}, {-1, 2}, {1, 2}};
bool dfs(int x, int y, int step, string path){
if(step == p*q){
cout << path << endl;
return true;
}
for(int i = 0; i < 8; i++){
int

该问题涉及使用深度优先搜索(DFS)策略解决在国际象棋坐标系统中的骑士移动问题。要注意的是,国际象棋坐标系统中,行用1-8表示,列用A-H表示,且方向与常规坐标系不同。理解这一特性后,应用深度优先搜索算法进行遍历即可。
最低0.47元/天 解锁文章
627

被折叠的 条评论
为什么被折叠?



