KnightsTour(启发式解决方案)

时间:2015-05-12 04:55:07

标签: java

我可以寻求帮助吗? 我有一个问题,即将此代码转换为启发式解决方案。我不知道应该用什么条件来使启发式解决方案有效。这只是随机移动,无法完成64次巡视。

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 } };

非常感谢您的帮助。

0 个答案:

没有答案