例5.8 - 跳马(全盘跳跃)

#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 << "输入";  

结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值