象棋王
2025-08-26 14:17:31
发布于:浙江
#include <bits/stdc++.h>
using namespace std;
struct Point {
int x, y;
Point(int x, int y) : x(x), y(y) {}
};
const int dx[4] = {2, 1, -1, -2};
const int dy[4] = {1, 2, 2, 1};
int n_bound, m_bound;
int targetX, targetY;
vector<vector<Point>> paths;
bool visited[11][11] = {false};
void dfs(int x, int y, vector<Point>& curPath) {
if (x == targetX && y == targetY) {
paths.push_back(curPath);
return;
}
for (int i = 0; i < 4; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx >= 0 && nx <= n_bound && ny >= 0 && ny <= m_bound) {
if (!visited[nx][ny]) {
visited[nx][ny] = true;
curPath.push_back(Point(nx, ny));
dfs(nx, ny, curPath);
curPath.pop_back();
visited[nx][ny] = false;
}
}
}
}
void printPaths() {
for (int i = 0; i < paths.size(); ++i) {
cout << setw(3) << i + 1 << ":";
for (int j = 0; j < paths[i].size(); ++j) {
if (j > 0) cout << "-->";
cout << paths[i][j].x << "," << paths[i][j].y;
}
cout << endl;
}
}
int main() {
int n, m;
cin >> n >> m;
n_bound = n;
m_bound = m;
targetX = n;
targetY = m;
vector<Point> curPath;
visited[2][1] = true;
curPath.push_back(Point(2, 1));
dfs(2, 1, curPath);
visited[2][1] = false;
curPath.pop_back();
visited[1][2] = true;
curPath.push_back(Point(1, 2));
dfs(1, 2, curPath);
visited[1][2] = false;
curPath.pop_back();
printPaths();
return 0;
}
全部评论 1
hao
1周前 来自 浙江
0
有帮助,赞一个