java题目:骑士周游

来源:百度知道 编辑:UC知道 时间:2024/05/30 01:45:13
尽可能让骑士不重复走完棋盘每一格,并向后计算一步,优先走较难达到的格子,难度一样则随机选择,以下程序可运行,但有极少步会出现优先格的错误选择,随机选择做的也不好,类与方法的安排也很混乱
各位高手请帮忙指出其中的错误,以及“高聚合低耦合”到底该怎样实现?
import java.util.Random;
public class Knight
{
private int currentRow;//当前所在行列
private int currentColum;
private int count;//计步
private int horizontal[]={ 2, 1, -1, -2, -2, -1, 1, 2 };//水平移动
private int vertical[]= { -1, -2, -2, -1, 1, 2, 2, 1 };//竖直移动

//移一步
public void move(boolean floor[][], int accessibility[][])
{
int tempRow;
int tempColum;//暂存行,列
int minRow=-1;//最小
int minColum=-1;
int a=0;
boolean flag=false;//是否继续
Random ra=new Random();

accessibility[currentRow][currentColum]--;//初始位置可达性减1

for (a=0; a<horizontal.length; a++)
{
tempRow=currentRow+vertical[a];
tempColum=currentColum+horizontal[a];//存入

if (tempRow<0 || tempRow>7 || tempColum<0

==========================================================
如果单纯只要解法,貌似用递归更好一点。。。
==========================================================
public class KnightTraval{
public static void main(String[] args){
long startTime=System.currentTimeMillis();
Knight me=new Knight(new Grid(0,0));
Grid[] result=me.getPath();
for(Grid g:result){
System.out.format("%1$c%2$d-->",g.column()+'A',g.row()+1);
}
System.out.println("结束");
System.out.format("耗时%1$d毫秒\n",System.currentTimeMillis()-startTime);
}
}
class Knight{
private Grid start;
private boolean[][] arrived;
public Knight(Grid start){
this.start=start;
arrived=new boolean[8][8];
}
public Grid[] getPath(){
Grid[] result=new Grid[64];
for(int i=0;i<6