KnightL on a Chessboard

Sort by

recency

|

131 Discussions

|

  • + 0 comments

    Here is solution in python, java, c++ and c programming = https://programmingoneonone.com/hackerrank-knightl-on-a-chessboard-problem-solution.html

  • + 0 comments

    Locks explores the concept of KnightL on a chessboard, illustrating strategic movement and problem-solving techniques. The knight’s unique L-shaped moves allow it to navigate obstacles, control key squares, and create tactical opportunities. Understanding its patterns enhances gameplay, planning, and foresight in chess. Practicing knight maneuvers improves critical thinking, spatial awareness, and decision-making skills. Mastery of the knight’s movements is essential for both beginners and advanced players seeking to strengthen their overall chess strategy.

  • + 0 comments

    c++

    struct cell{
        int x, y, dis;
        cell(): x(0), y(0), dis(0) {}
        cell(int x, int y, int dis) : x(x), y(y), dis(dis) {}
    };
    
    bool inBoard(int x, int y, int n){
        return x>=0 && x<n && y>=0 && y<n;
    }
    
    
    int steps(int a, int b, int n){
        int dx[8] = { a,  a, -a, -a,  b,  b, -b, -b };
        int dy[8] = { b, -b,  b, -b,  a, -a,  a, -a };
        
        vector<vector<bool>> visited(n+1, vector<bool>(n+1, false));
        visited[0][0] = true;
        
        queue<cell> q;
        q.push(cell(0, 0, 0));
        
        cell t;
        int x, y;
        
        while (!q.empty()) {
            t = q.front();
            q.pop();
            
            if(t.x == n-1 && t.y == n-1) return t.dis;
            
            for(int i = 0; i<8; i++){
                x = t.x + dx[i];
                y = t.y + dy[i];
                
                if(inBoard(x, y, n) && !visited[x][y]){
                    visited[x][y] = true;
                    q.push(cell(x, y, t.dis+1));
                }
            }
        
        }
        
        return -1;
    }
    
    vector<vector<int>> knightlOnAChessboard(int n) {
        vector<vector<int>> result(n-1, vector<int>(n-1, 0));
        
        for(int i = 1; i<n; i++){
            for(int j = 1; j<n; j++){
                result[i-1][j-1] = steps(i, j, n);
            }
        }
        
        return result;
    }
    
  • + 0 comments

    using BFS its done , but can it be done using DFS? or backtracking?

  • + 0 comments

    Java with optimisations: do only half, as output is symmetrical, special cases i==j, greatest common factor of i,j does not divide n-1.

    import java.io.*;
    import java.math.*;
    import java.security.*;
    import java.text.*;
    import java.util.*;
    import java.util.concurrent.*;
    import java.util.function.*;
    import java.util.regex.*;
    import java.util.stream.*;
    import static java.util.stream.Collectors.joining;
    import static java.util.stream.Collectors.toList;
    
    class Result {
    
        /*
         * Complete the 'knightlOnAChessboard' function below.
         *
         * The function is expected to return a 2D_INTEGER_ARRAY.
         * The function accepts INTEGER n as parameter.
         */
    
        public static int[][] states;
        public static int[] tmp;
        
    
        public static int gcf(int i, int j) {
            int k = (i > j)? j : i;
            int m = (i > j)? i : j;
            while (k > 1) {
              int q = m - k;
              if (q == 0) {
                 return k;
              }
              if (q == 1) {
                return 1;
              }
              if (q > k) {
                m = q;
              } else {
                int w = k;
                k = q;
                m = w;
              }
            }
            return k;
        }
    
    
        public static Integer ket(int i, int j, int d) {
            return i * 65536  + j * 256 +  d;
        }
        
        public static  void fromk(Integer k) {
            int a = k / 65536;
            int b = (k - a * 65536 )  / 256;
            int c = k - a * 65536 - b * 256;
            tmp[0] = a;
            tmp[1] = b;
            tmp[2] = c;
        }
    
        public static void setstates(int i, int j) {
            states[0][0] = -i;
            states[0][1] = -j;
            
            states[1][0] = -i;
            states[1][1] = j;
            
            states[2][0] = i;
            states[2][1] = -j;
            
            states[3][0] = i;
            states[3][1] = j;
            
            states[4][0] = -j;
            states[4][1] = -i;
            
            states[5][0] = -j;
            states[5][1] = i;
            
            states[6][0] = j;
            states[6][1] = -i;
            
            states[7][0] = j;
            states[7][1] = i;
        }
        
        public static boolean checkpos(int x, int y, int n) {
            return (x >= 0 && y >= 0 && x < n && y < n);
        }
        
    
        public static Integer getk(int i, int j) {
            return i * 256 + j;
        }
    
        public static int bfs(int i, int j, int n) {        
            Set<Integer> seen = new HashSet<>();
            Queue<Integer> todo = new LinkedList<>();
    
            todo.add(ket(0, 0, 0));
            seen.add(getk(0, 0));
            while (! todo.isEmpty()) {
                fromk(todo.remove());
                int x  = tmp[0];
                int y = tmp[1];
                int d = tmp[2];
                if (x == n - 1 && y == n - 1) {
                    return d;
                }
                for (int q = 0; q < 8; q++) {
                    int xx = x + states[q][0];
                    int yy  = y + states[q][1];
                    if (checkpos(xx, yy, n)) {
                        Integer kk = getk(xx, yy);
                        if (! seen.contains(kk)) {
                            seen.add(getk(xx, yy));
                            todo.add(ket(xx, yy, d+1));
                        }
                    }
                }
            }
            return -1;
        }
    
        public static List<List<Integer>> knightlOnAChessboard(int n) {
            // BFS.
            tmp = new int[3];
    
            // Since output is symmetrical, do upper half:
            states = new int[8][2];
            int[][] resulti = new int[n][n];
            for (int i = 1; i < n; i++) {
                for (int j = i; j < n; j++) {
                    // optimize if i==j
                    if (i == j && ((n - 1) % i == 0 || i ==1) ) {
                            resulti[i][j] = (n  - 1)/ i; 
                    } else {
                        int f = gcf(i, j);
                        // optimize greatest common factor cannot reach (n-1, n-1):
                        if (f != 1 && (n - 1) % f != 0) {
                            resulti[i][j] = -1;
                        } else {
                            setstates(i, j);
                            resulti[i][j] = bfs(i, j, n);
                        }
                    }
                }
            }
    
            // Since output is symmetrical, copy upper half to lower half:
            List<List<Integer>> result = new ArrayList<>();
            for (int i = 1; i < n; i++) {
                List<Integer> row = new ArrayList<>();
                for (int j = 1; j < n; j++) {
                   int x = (i > j) ? i : j;
                   int y = (i > j) ? j : i;
                   row.add(resulti[y][x]);
                }
                result.add(row);
            }
            return result;
        }
    }
    
    public class Solution {
        public static void main(String[] args) throws IOException {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
    
            int n = Integer.parseInt(bufferedReader.readLine().trim());
    
            List<List<Integer>> result = Result.knightlOnAChessboard(n);
    
            result.stream()
                .map(
                    r -> r.stream()
                        .map(Object::toString)
                        .collect(joining(" "))
                )
                .map(r -> r + "\n")
                .collect(toList())
                .forEach(e -> {
                    try {
                        bufferedWriter.write(e);
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                });
    
            bufferedReader.close();
            bufferedWriter.close();
        }
    }