在一个 m*n(m、n 都小于等于 100)的方格中,一只马可 以按日走,也可以按田走,从右上角走到左下角,请输出马能走的所有路径、路 径总数、最短路径以及最短路径长度

在一个 m*n(m、n 都小于等于 100)的方格中,一只马可
以按日走,也可以按田走,从右上角走到左下角,请输出马能走的所有路径、路
径总数、最短路径以及最短路径长度
输入:
3 3
输出:
(1,3)->(3,2)->(1,1)->(2,3)->(3,1)
(1,3)->(3,2)->(1,1)->(3,3)->(1,2)->(3,1)
(1,3)->(2,1)->(3,3)->(1,2)->(3,1)
(1,3)->(2,1)->(3,3)->(1,1)->(2,3)->(3,1)
(1,3)->(3,1)
5
(1,3)->(3,1)
1


#include <iostream>
#include <vector>

using namespace std;

const int N = 105;
int m, n;
int dx[8] = {-2, -2, -1, -1, 1, 1, 2, 2};
int dy[8] = {-1, 1, -2, 2, -2, 2, -1, 1};
vector<pair<int, int>> path;
int res = 0;

bool inArea(int x, int y) {
    return x >= 0 && x < m && y >= 0 && y < n;
}

void dfs(int x, int y) {
    if (!inArea(x, y)) return;
    if (x == m - 1 && y == n - 1) {
        res++;
        for (auto p : path) cout << "(" << p.first + 1 << "," << p.second + 1 << ")->";
        cout << "(" << x + 1 << "," << y + 1 << ")" << endl;
        return;
    }
    path.push_back({x, y});
    for (int i = 0; i < 8; i++) {
        int a = x + dx[i];
        int b = y + dy[i];
        dfs(a, b);
    }
    path.pop_back();
}

int main() {
    cin >> m >> n;
    dfs(0, 0);
    cout << res << endl;
    return 0;
}

以下答案引用自GPT-3大模型,请合理使用:

#include <iostream> #include <string> using namespace std; int main() { int m,n,i,j,k,l,path[6][2]={ {1,3},{3,2},{1,1},{2,3},{3,1},{1,2} }; for(m=1;m<=n;m++) { for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { if(i==j) continue; path[i][j]=0; } } } cout<<"马能走的所有路径为:

"<<endl; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { if(path[i][j]) cout<<"("<<i<<","<<j<<")

"; } } cout<<"路径总数为: "<<path[1][n]<<endl; cout<<"最短路径长度为: "<<path[1][n]<<endl; return 0; }