#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;
int u[8] = {1,1,2,2,-1,-1,-2,-2}; //互相对应
int v[8] = {2,-2,1,-1,2,-2,1,-1};
int a[6][6] = {0};
int total=0;
void print(){
if(total<=5){
for(int i=1;i<=5;i++){
for(int j=1;j<=5;j++){
cout << a[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
}
total++;
}
void skip(int h,int w,int n){ //(n-1)棋子的坐标为[h][w],此时放第n个棋子
int x,y,k; //临时变量 -> 只需恢复1步
for(int i=0;i<8;i++){
x=h+v[i];
y=w+u[i];
if(x<=5 && x>=1 && y<=5 && y>=1 && !a[x][y]){ //1.条件:①不超界②无棋子
a[x][y] = n; //占用棋子
if(n==25){print();} //2.目的
else{skip(x,y,n+1);} //搜索
a[x][y] = 0; //3.回溯恢复
}
}
}
int main()
{
//流程图:分析 >> 敲码 -> !不要用战术上的勤奋掩盖战略上的懒惰!
a[1][1] = 1; //第一个(1,1)
skip(1,1,2);
cout << total;
return 0;
}
//cout << "输入";
例5.8 - 跳马(全盘跳跃)
最新推荐文章于 2023-02-16 16:41:51 发布