KnightL on a Chessboard

Sort by

recency

|

125 Discussions

|

  • + 0 comments

    Promotional merchandise for "KnightL on a Chessboard" can bring the classic strategy of chess to life. Custom chess sets featuring your brand’s logo or personalized pieces add a touch of sophistication. Branded chessboards, stylish keychains shaped like knights, or limited-edition chess piece stress balls can make thoughtful giveaways. Engage enthusiasts with these elegant items, perfect for corporate events or promotions that highlight strategy, skill, and timeless appeal.

  • + 0 comments

    The Knight's unique movement in chess, leaping in an "L" shape, allows it to control both near and distant squares, making it a versatile piece on the board; you can read more about the fascinating strategies involving the Knight at https://awomansjourney.com.

  • + 0 comments
    def knightlOnAChessboard(n):
        def bfs(n, a, b):
            directions = [(a, b), (a, -b), (-a, b), (-a, -b),
                          (b, a), (b, -a), (-b, a), (-b, -a)]
            queue = deque([(0, 0, 0)])
            visited = set([(0, 0)])
            
            while queue:
                x, y, dist = queue.popleft()
                if x == n-1 and y == n-1:
                    return dist
                
                for dx, dy in directions:
                    nx, ny = x + dx, y + dy
                    if 0 <= nx < n and 0 <= ny < n and (nx, ny) not in visited:
                        visited.add((nx, ny))
                        queue.append((nx, ny, dist + 1))
            
            return -1
    
        result = []
        for i in range(1, n):
            row = []
            for j in range(1, n):
                row.append(bfs(n, i, j))
            result.append(row)
        
        return result
    
  • + 0 comments

    C++ BFS approach (https://github.com/IhorVodko/Hackerrank_solutions/blob/master/Algorithms/knightLOnAChessboard.cpp, feel free to give a star :) )

  • + 0 comments
    def knightlOnAChessboard(n):
        
        def get_moves(position:tuple, a:int, b:int, cost:int):
            x, y = position 
            moves_x = {"a":[],"b":[]}
            moves_y = {"a":[],"b":[]} 
            
            for coordinate, moves in zip([x,y], [moves_x,moves_y]):
              for delta, final_moves in zip([a,b], [moves["a"], moves["b"]]):
                if (coordinate+delta <= n-1):
                    final_moves.append(coordinate+delta) 
                if (coordinate-delta >= 0): 
                    final_moves.append(coordinate-delta)
            
            moves_a_b = [(x,y, cost + 1) for x in moves_x["a"] for y in moves_y["b"]]
            moves_b_a = [(x,y, cost + 1) for x in moves_x["b"] for y in moves_y["a"]]
            
            return moves_a_b + moves_b_a
    
        def ucs(queue:list, visited:list, a:int, b:int):
            while (len(queue)):
              x,y, cost = queue.pop(0)
              if (x,y) == (n-1, n-1):
                return cost
              if (x,y) not in visited:
                visited.append((x,y))
                moves = get_moves((x,y),a,b, cost)
                queue.extend(moves)
            return -1
                
        results=[]
        for i in range(1,n):
            list_results=[]
            for j in range(1,n):
                cost = ucs([(0,0,0)], [], i,j)
                list_results.append(cost)
            results.append(list_results)
            
        return results