import java.util.Random;
public class Knight
{
Random rand=new Random();
public void start()
{
int[][] square = new int[8][8];
int currentRow;
int currentColumn;
int x,y,count,count1=0;
int moveNumber;
int [] horizontal={2,1,-1,-2,-2,-1,1,2};
int [] vertical={-1,-2,-2,-1,1,2,2,1};
for(x=0;x<8;x++)
{
for(y=0;y<8;y++)
{
square[x][y]=0;
}
}
currentRow=rand.nextInt(7);
currentColumn=rand.nextInt(7);
square[currentRow][currentColumn]=1;
for(count=2;count<=64;count++)
{
for(moveNumber=0;moveNumber<8;moveNumber++)
{
currentRow+=vertical[moveNumber];
currentColumn+=horizontal[moveNumber];
if(currentColumn >= 0 && currentColumn < 8 && currentRow >= 0 && currentRow < 8 && square[currentRow][currentColumn]==0)
{
square[currentRow][currentColumn]=count;
count1++;
break;
}
else
{
currentRow-=vertical[moveNumber];
currentColumn-=horizontal[moveNumber];
}
}
}
System.out.printf("The tour ended with %d moves\n \t0\t1\t2\t3\t4\t5\t6\t7\n",count1+1);
for(x=0;x<8;x++)
{
System.out.printf("%d\t", x);
for(y=0;y<8;y++)
{
System.out.printf("%d\t",square[x][y]);
}
System.out.println();
}
}
}
如果我要使用启发式解决方案,我需要在下面添加此代码,但我没有线索如何继续更改
int access[ ][ ] = { { 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 } };
非常感谢您的帮助。