0

https://www.hackerrank.com/challenges/knightl-on-chessboard/problem This is the problem that I have been trying to solve using simple loops and recursive approach and trying to keep my code simple void of any boolean or queues etc. My code is working but has very high time complexity.

import javax.swing.*;


public class lknightmycode {

    public static boolean canreach(int[][] board, int row, int col) {
      if(row>=0 && col>=0 && row< board.length&&col< board.length&&board[row][col]==-1){
          return true;
      }
        else return false;
    }

    public static int helper(int[][] board, int row, int col, int crow, int ccol, int steps) {
         if (crow == board.length - 1 && ccol == board.length - 1){
                return steps;}
        if (canreach(board, crow, ccol)) {
            board[crow][ccol] = 0;
            
           
                    int minSteps = Integer.MAX_VALUE;
                    int[] dr = {-row, -row, row, row,col, -col, col, -col};
                    int[] dc = {col, -col, col, -col,-row, -row, row, row};

                    for (int i = 0; i < 8; i++) {
                        int newRow = crow + dr[i];
                        int newCol = ccol + dc[i];

                        int currentSteps = helper(board, row, col, newRow, newCol, steps + 1);
                        if (currentSteps != -1) {
                            minSteps = Math.min(minSteps, currentSteps);
                        }
                    }
                board[crow][ccol] = -1; // Reset the board position

                    return (minSteps == Integer.MAX_VALUE) ? -1 : minSteps;
                }
            
            return -1;
        }
    public static int[][] knightlOnAChessboard(int n) {
        int[][] board = new int[n][n];
        int[][] Result = new int[n - 1][n - 1];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                board[i][j] = -1;
            }}
        for (int i = 0; i < n-1; i++) {
            for (int j = 0; j<=i; j++) {

                    Result[i][j] = helper(board, i + 1, j + 1, 0, 0, 0);
                    Result[j][i]=Result[i][j];
                    board = new int[n][n];
                    for (int x = 0; x < n; x++) {
                        for (int y = 0; y < n; y++) {
                            board[x][y] = -1;

                    }
                }
            }}

        return Result;

    }

    public static void main(String[] args) {
        int n = 5;
        int[][] result = knightlOnAChessboard(n);
        for (int i = 0; i < n-1; i++) {
            for (int j = 0; j < n-1; j++) {
                System.out.print(result[i][j] + " ");
            }
            System.out.println();
        }
    }
}

This is the code I have written. It is working fine for values of n upto 5 but is having very high time complexity for n=6 and above. Please guide me on how to make the code less time complex and please consider me as a beginner and explain accordingly.

  • Off-topic: When I see a statement like *"I don't want to complicate the code by adding queues, stacks, lists etc."*, I think "What if adding a stack *simplifies* the code?". I wonder how *"the simplest possible code irrespective of the time complexity"* is measured. Without some standard, "the simplest possible code" could be a matter of opinion. Some might even regard it as a code golf question. – Old Dog Programmer May 25 '23 at 14:34
  • Off-Topic: Note: The part of the code provided by HackerRank is modified from the original problem. HackerRank has the return type of `knightlOnAChessboard(int n)` as `List>`, while here it is `int [][]`. – Old Dog Programmer May 25 '23 at 14:43

0 Answers0