java - KnightsTour(Heuristic Solution) -
can ask help? have problem of converting code heuristic solution. don't know condition should put make heuristic solution works.this random move not complete 64 tours.
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 %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(); } } }
if use heuristic solution,i need add code below have no clues how continue changing it
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 } };
much appreciate helping.
Comments
Post a Comment