回溯法-马踏棋盘

问题描述
将马随机放在国际象棋的Board[0~7][0~7]的某个方格中,马按走棋规则进行移动。,走遍棋盘上全部64个方格。编制非递归程序,求出马的行走路线,并按求出的行走路线,将数字1,2,…,64依次填入一个8×8的方阵,输出之。
解题思路
我们用一个二维数组模拟马走的方向,通过函数move(x,y),来达到马走。 如果棋盘next_x>=0 && next_x<=4 && next_y>=0 && next_y<=3表示next_x,next_y在棋盘内,棋盘qipan[next_x][next_y] = = 0表示起盘没有走过,即下一步满足这些条件,则把下一步置为走过的状态,step++,调用move(next_x,next_y)函数,调用完再回溯。
代码

package TanXin;
import java.util.Scanner;

public class MataQipan {
    //马能走的8个方向
static int weizhi[][] = {{-2,1},{-2,-1},{-1,2},{-1,-2},{1,2},{1,-2},{2,1},{2,-1}};

static int step = 1;//先走哪一步,步数
static int qipan[][] = new int[5][4];
static int count = 0;
public static void main(String[] args) {
    //初始化棋盘
    for(int i=0;i<qipan.length;i++){
        for(int j=0;j<qipan[i].length;j++){
            qipan[i][j] = 0;
        }
    }
    //输入起始位置,起始位置从1,1开始算 ,计算机数组从0,0开始算,所以x--,y--;
    Scanner scn = new Scanner(System.in);
    System.out.println("请输入棋盘马起始位置:");
    int x = scn.nextInt();
    int y = scn.nextInt();

    qipan[x][y] = step;
    step++;
    move(x,y);
    System.out.println("共有" + count + "种方案");
}
public static void move(int x, int y) {
    int next_x = 0;
    int next_y = 0;
    if(step>20){
        for(int i=0;i<5;i++){
            for(int j=0;j<4;j++){
                System.out.printf("%5d",qipan[i][j]);
            }
            System.out.println();
        }
        System.out.println("==========================");
        count ++;
        return;
    }else{
        for(int i=0;i<8;i++){
            next_x = x + weizhi[i][0];
            next_y = y + weizhi[i][1];

//下一步棋满足条件
            if(next_x>=0 && next_x<=4 && next_y>=0 && next_y<=3 && qipan[next_x][next_y] ==0){
                qipan[next_x][next_y] = step;
                step++;
                move(next_x,next_y);
                step--;
                qipan[next_x][next_y] = 0;
            }
        }
    }
}
}

运行结果

请输入棋盘马起始位置:
0
0
    1    4   17   12
   18   13    2    5
    3    8   11   16
   14   19    6    9
    7   10   15   20
==========================
    1    4   15   20
   14   19    2    5
    3    8   11   16
   18   13    6    9
    7   10   17   12
==========================
    1   10   17   12
   18   13    2    9
    3    6   11   16
   14   19    8    5
    7    4   15   20
==========================
    1   10   15   20
   14   19    2    9
    3    6   11   16
   18   13    8    5
    7    4   17   12
==========================
    1    8   17   12
   18   13    2    7
    9    4   11   16
   14   19    6    3
    5   10   15   20
==========================
    1    8   15   20
   14   19    2    7
    9    4   11   16
   18   13    6    3
    5   10   17   12
==========================
    1    6   17   12
   18   11    2    7
    5    8   13   16
   14   19   10    3
    9    4   15   20
==========================
    1    6   17   12
   16   11    2    7
    5    8   13   18
   20   15   10    3
    9    4   19   14
==========================
    1    6   15   20
   16   11    2    7
    5    8   19   14
   12   17   10    3
    9    4   13   18
==========================
    1    6   11   14
   12   15    2    7
    5   10   13   18
   16   19    8    3
    9    4   17   20
==========================
    1    6   11   20
   12   19    2    7
    5   10   13   16
   18   15    8    3
    9    4   17   14
==========================
    1    6   11   18
   12   17    2    7
    5   10   19   14
   16   13    8    3
    9    4   15   20
==========================
    1    6   11   16
   12   17    2    7
    5   10   15   18
   20   13    8    3
    9    4   19   14
==========================
    1    6   11   16
   12   17    2    7
    5   10   15   20
   18   13    8    3
    9    4   19   14
==========================
    1    6   11   20
   12   19    2    7
    5   10   15   18
   16   13    8    3
    9    4   17   14
==========================
    1    6   11   16
   18   15    2    7
    5   10   17   12
   14   19    8    3
    9    4   13   20
==========================
    1    6   11   16
   20   15    2    7
    5   10   17   12
   14   19    8    3
    9    4   13   18
==========================
    1    6   13   18
   14   19    2    7
    5   10   17   12
   20   15    8    3
    9    4   11   16
==========================
    1    6   13   20
   14   19    2    7
    5   10   17   12
   18   15    8    3
    9    4   11   16
==========================
    1    6   17   14
   16   13    2    7
    5   10   15   18
   12   19    8    3
    9    4   11   20
==========================
    1    6   19   14
   18   13    2    7
    5   10   15   20
   12   17    8    3
    9    4   11   16
==========================
    1    6   19   14
   20   13    2    7
    5   10   15   18
   12   17    8    3
    9    4   11   16
==========================
    1    6   15   20
   16   13    2    7
    5   10   19   14
   12   17    8    3
    9    4   11   18
==========================
    1    6   17   14
   18   15    2    7
    5   10   13   16
   12   19    8    3
    9    4   11   20
==========================
    1    6   17   20
   16   19    2    7
    5   10   13   18
   12   15    8    3
    9    4   11   14
==========================
    1   10   17   12
   18   13    2    9
    5    8   11   16
   14   19    6    3
    7    4   15   20
==========================
    1   10   15   20
   14   19    2    9
    5    8   11   16
   18   13    6    3
    7    4   17   12
==========================
    1    6   17   12
   18   13    8    5
    7    2   11   16
   14   19    4    9
    3   10   15   20
==========================
    1    6   15   20
   14   19    8    5
    7    2   11   16
   18   13    4    9
    3   10   17   12
==========================
    1    6   13   18
   12   17   10    5
    7    2   19   14
   16   11    4    9
    3    8   15   20
==========================
    1    6   19   14
   20   15   10    5
    7    2   13   18
   16   11    4    9
    3    8   17   12
==========================
    1    6   15   20
   14   19   10    5
    7    2   13   16
   18   11    4    9
    3    8   17   12
==========================
共有32种方案

猜你喜欢

转载自blog.csdn.net/xie__jin__cheng/article/details/90901399