骑士旅行问题 最小访问数试探法

本文介绍了一个使用Java实现的骑士旅行问题解决方案。通过定义棋盘和骑士可能的移动方式,程序能够找到骑士遍历整个棋盘的方法,并记录下每一步的路径。该应用还包含了图形界面来展示骑士移动的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package homeWork;

import java.awt.Container;

import java.awt.FlowLayout;

import javax.swing.JApplet;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

public class KnightTravel extends JApplet {//骑士旅行 7.22

private static final long serialVersionUID = 1L;

int horizontal[] = {2,1,-1,-2,-2,-1,1,2};//表示移动的方向和步数 向上和向左为负

int vertical[] = {-1,-2,-2,-1,1,2,2,1};

int moveNumber;//移动的方式 0--7

int board[][] = new int[8][8];//棋盘

int Xboard[][] = {{2,3,4,4,4,4,3,2},{3,4,6,6,6,6,4,3},{4,6,8,8,8,8,6,4},{4,6,8,8,8,8,6,4},{4,6,8,8,8,8,6,4},

{4,6,8,8,8,8,6,4},{3,4,6,6,6,6,4,3},{2,3,4,4,4,4,3,2}};//记录方格访问难易程度,数值越小越难访问

int moveRecord;//记录当前的选择

int footSteps;//记录骑士移动的步数

int currentRow = 0;

int currentCol = 0;

int s = 1;//记录成功次数

int Record = 0;//记录骑士当前移动的最远步数

int row,col;//记录当前应移动的行和列

int XchangeRow,XchangeCol;//记录访问数减少的行和列

boolean R;//记录移动是否成功

boolean moveJudge;//判断是否可以移动

JTextArea outpuTextArea;

String outputString = "";

JScrollPane scrollPane;

public void init(){

Container container = getContentPane();

container.setLayout(new FlowLayout());

outpuTextArea = new JTextArea(17,20);

scrollPane = new JScrollPane(outpuTextArea);

container.add(scrollPane);

for(currentRow = 0;currentRow < 8;currentRow ++)

for(currentCol = 0;currentCol < 8;currentCol ++){

R = move(board,currentRow,currentCol);//移动骑士

Print(board,R);//打印结果

}

}

// 骑士移动过程

public boolean move(int array[][],int currentRow,int currentCol){

// int currentRow = 0;//当前行

// int currentCol = 0;//当前列

// 初始化操作

for(int row = 0;row < 8;row++)

for(int col = 0;col < 8;col ++)

board[row][col] = 0;

int Xboard[][] = {{2,3,4,4,4,4,3,2},{3,4,6,6,6,6,4,3},{4,6,8,8,8,8,6,4},{4,6,8,8,8,8,6,4},{4,6,8,8,8,8,6,4},

{4,6,8,8,8,8,6,4},{3,4,6,6,6,6,4,3},{2,3,4,4,4,4,3,2}};

Record = 0;

footSteps = 1;

int tempCR,tempCC;//保存当前行当前列

board[currentRow][currentCol] = footSteps++;

while(footSteps<=64){

moveNumber = 0;

moveRecord = 9;

moveJudge = false;

while(moveNumber < 8){//找出当前最小访问数

// if(moveNumber > 7)

// return false;

tempCR = currentRow;

tempCC = currentCol;

currentRow += vertical[moveNumber];

currentCol += horizontal[moveNumber];

if(currentRow <8 && currentRow >= 0 && currentCol >= 0 && currentCol < 8 //是否越过棋盘

&& board[currentRow][currentCol] == 0){//此位置是否已经经过

moveJudge = true;

if(Xboard[currentRow][currentCol] < moveRecord){

moveRecord = Xboard[currentRow][currentCol];

row = currentRow;

col = currentCol;

}

}

// board[row][col] = footSteps;

// Record++;

// break;

currentRow = tempCR;//返回原来位置

currentCol = tempCC;

moveNumber ++;

}

if(moveJudge){

currentRow = row;//保存当前位置

currentCol = col;

board[row][col] = footSteps;

Record++;

footSteps++;

moveNumber = 0;

XchangeRow = row;

XchangeCol = col;

while(moveNumber < 8){

tempCR = XchangeRow;

tempCC = XchangeCol;

XchangeRow += vertical[moveNumber];

XchangeCol += horizontal[moveNumber];

if(XchangeRow <8 && XchangeRow >= 0 && XchangeCol >= 0 && XchangeCol < 8//是否越过棋盘

&& board[XchangeRow][XchangeCol] == 0)//此位置是否已经经过

Xboard[XchangeRow][XchangeCol]--;

XchangeRow = tempCR;

XchangeCol = tempCC;

moveNumber ++;

}

}

else

return false;

}

return true;

}

// 打印移动结果

public void Print(int Parray[][],boolean result){

if(result){

for(int row = 0;row < 8;row++){

for(int col = 0;col < 8;col ++)

outputString += board[row][col]+" ";

outputString += "/n";

}

outputString += "success! "+ s + "/n";

s++;

}

else {

for(int row = 0;row < 8;row++){

for(int col = 0;col < 8;col ++)

outputString += board[row][col]+" ";

outputString += "/n";

}

outputString +=" Move False!"+" "+Record +"/n";

}

outpuTextArea.setText(outputString);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值