not the typical knights tour problem! please read all before submitting the gene
ID: 3773096 • Letter: N
Question
not the typical knights tour problem! please read all before submitting the generic knights tour answer.
Also heres a majority of the work, im getting errors with the possibles. i think there should be another class
import java.util.*;
public class Drunk
{
public static Random r1 = new Random();
static ArrayList<Integer> AList = new ArrayList<Integer>();
public static int[][] ktour = new int[8][8];
public static int count = 0;
public static int klongest = 0;
public static int counter = 0;
public static void main(String[] args)
{
int[][] kboard = new int[8][8];
for(int a = 0; a<8; a++)
{
for(int b = 0; b<8; b++)
{
kboard[a][b] = 1;
ktour[a][b] = 0;
}
}
while(!kcomplete(ktour))
{
counter++;
for(int a = 0; a<8; a++)
{
for(int b = 0; b<8; b++)
{
kboard[a][b] = 1;
ktour[a][b] = 0;
}
}
count = 0;
kTest(kboard,r1.nextInt(8),r1.nextInt(8));
if(counter <= 5 || kcomplete(ktour) || (count > klongest && counter >5) || counter%1000 == 0)
{
System.out.println(counter+" tour, tour legnth: " + count);
System.out.println("");
display(ktour);
if(count > klongest)
{
klongest = count;
}
}
if(counter == 1000000)
{
System.out.println("After 1000000 tours, longest tour" + klongest + "moves long & we've yet to have a complete-tour");
break;
}
}
}
public static boolean kcomplete(int[][] kboard)
{
for(int a = 0; a<8; a++)
{
for(int b = 0; b<8; b++)
{
if(kboard[a][b] == 0)
return false;
}
}
return true;
}
public static void display(int[][] kboard)
{
for(int a = 0; a<8; a++)
{
for(int b = 0; b<8; b++)
{
System.out.print(" " + kboard[a][b] + " ");
}
System.out.println(" ");
}
}
public static void kTest(int[][] kboard,int a, int b)
{
int krandom = 0;
int size = 0;
kboard[a][b] = 0;
ktour[a][b] = ++count;
possibles p = new possibles();
AList = p.possibles(kboard,a,b);
size = AList.size();
if(!AList.isEmpty())
{
if(size == 1)
{
krandom = 0;
}
else
{
krandom = r1.nextInt(size);
}
if(AList.get(krandom) == 1)
{
kTest(kboard,a-2,b+1);
}
if(AList.get(krandom) == 2)
{
kTest(kboard,a-1,b+2);
}
if(AList.get(krandom) == 3)
{
kTest(kboard,a+1,b+2);
}
if(AList.get(krandom) == 4)
{
kTest(kboard,a+2,b+1);
}
if(AList.get(krandom) == 5)
{
kTest(kboard,a+2,b-1);
}
if(AList.get(krandom) == 6)
{
kTest(kboard,a+1,b-2);
}
if(AList.get(krandom) == 7)
{
kTest(kboard,a-1,b-2);
}
if(AList.get(krandom) == 8)
{
kTest(kboard,a-2,b-1);
}
}
}
}
Explanation / Answer
public class TheKnigthsTour { private final static int BOARD_LENGTH = 8; //The length of the board private static int board[][]; //The simulated board //List of possible moves for the knight private static final Point[] MOVES = new Point[]{new Point(-2, -1), new Point(-2, 1), new Point(2, -1), new Point(2, 1), new Point(-1, -2), new Point(-1, 2), new Point(1, -2), new Point(1, 2)}; public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Enter starting row (0-7): "); int row = in.nextInt(); System.out.printf("Enter starting column (0-7): "); int col = in.nextInt(); solveTour(row, col); } /** * Helper method to determine if a square is safe for the knight * * @param row the row the knight is on * @param col the column the knight is on * @return true if the square is safe for the knight */ private static boolean isSafe(int row, int col) { return ((row >= 0 && row = 0 && colRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.