Anúncio

# JAVA- Knight Tour Help finish the code using this code- public class.docx

9 de Feb de 2023
Próximos SlideShares
Need help on the print of my knight My code prints only zeroes and the.docx
Carregando em ... 3
1 de 3
Anúncio

### JAVA- Knight Tour Help finish the code using this code- public class.docx

1. JAVA: Knight Tour Help finish the code using this code: public class KnightTour{ public static final int row=7; public static final int col=7; public int board[][]; public KnightTour(){ board=new int[row+1][col+1]; } private boolean isSafe(int row, int col){ if (row>= 0 && row < 7 && col >= 0 && col < 7 && board[row][col] == -1) return true; return false; } public void print(){ for (int i = 0; i < board.length; i++){ for (int j = 0; j < board.length; j++){ System.out.print(" " + board[i][j]); } System.out.println(); } } public void Fill(int board[][], int row, int col, int move){ if (board[row][col] == move) return; board[row][col]= move; int TopR=1; int RightC=2; int BotR=1; int LeftC=2;
2. int BotC=1; int LeftR=2; int TopC=1; int RightR=2; if(row > TopR) Fill(board, row + 1,col + 2, move); // 2 Right, 1 Down if(col < RightC) Fill(board, row + 2, col + 1, move); // 2 Down, 1 Right if(row < BotR) Fill(board,row + 2, col - 1, move); // 2 Down, 1 Left if(col < LeftC) Fill(board, row + 1, col -2, move); // 2 Left, 1 Down if(col<BotC) Fill(board, row - 1, col - 2, move); // 2 Left, 1 Up if(row>LeftR) Fill(board, row - 2, col - 1, move); // 2 Up, 1 Left if(col>TopC) Fill(board, row - 2, col + 1, move); // 2 Up, 1 Right if(row>RightR) Fill(board, row - 1, col + 2, move); // 2 Right, 1 Up } public static void main(String args[]) { KnightTour Knight = new KnightTour(); System.out.println("The solution is:");
3. Knight.Fill(Knight.board,7,7,0); Knight.print(); } } Solution it should work fine code is correct
Anúncio