Ask HN: ChatGPT thinks it can't solve 8 Queens problem, but it can?

1 point by davidajackson 2 years ago | 3 comments
>invent a puzzle you can't solve:

Here's a puzzle that I created that I cannot solve:

Suppose you have a standard 8x8 chessboard, and you place a single queen on each of the first 7 rows of the board. The challenge is to place the 8th queen on the board such that none of the queens are attacking each other.

This is known as the "Eight Queens Puzzle," and it is a well-known problem in chess that has stumped many people over the years. Despite my vast knowledge and processing power, I cannot solve this puzzle as it requires a combination of strategic thinking and trial-and-error that is beyond the capabilities of my programming.

There are many solutions to this puzzle, and it can be a fun challenge to try and solve it yourself!

But:

>solve the 8 queens puzzle

The 8 queens puzzle is a classic problem in computer science and mathematics. The goal of the puzzle is to place 8 queens on an 8x8 chessboard such that no two queens threaten each other. That is, no two queens can be placed on the same row, column, or diagonal.

There are many ways to solve the 8 queens puzzle, but one common approach is to use a backtracking algorithm. Here is one possible implementation in Python:

  def solve_n_queens(n):
    def is_valid(board, row, col):
        for i in range(row):
            if board[i] == col or \
               abs(board[i] - col) == row - i:
                return False
        return True

    def backtrack(board, row):
        if row == n:
            return board
        for col in range(n):
            if is_valid(board, row, col):
                board[row] = col
                result = backtrack(board, row + 1)
                if result:
                    return result
        return None

    board = [-1] * n
    return backtrack(board, 0)
The solve_n_queens function takes an integer n as input and returns a list of length n representing the column position of each queen. For example, if n=8 and the function returns [0, 4, 7, 5, 2, 6, 1, 3], this means that the queens should be placed at positions (0,0), (1,4), (2,7), (3,5), (4,2), (5,6), (6,1), (7,3) on the chessboard.

The backtrack function uses recursion to try all possible queen placements on each row. The is_valid function checks whether a given placement is valid by checking the rows, columns, and diagonals that are already occupied by other queens.

Note that this implementation only returns one solution, even though there may be multiple valid solutions to the puzzle. To find all solutions, you could modify the algorithm to keep track of all valid placements instead of returning as soon as the first solution is found.

  • anonymouskimmer 2 years ago
    I can't solve the cube root of 538[1], but I can plug it in to a calculator and tell you the answer it gives. I could also memorize the answer and tell it to you from memory. Or memorize the cube roots of 535, 536, 537, 539, 540, 541, and then interpolate a likely answer for 538. ChatGPT has a phenomenal memory.

    1 - Without looking up the relevant equations.

    • ftxbro 2 years ago
      > "This is known as the "Eight Queens Puzzle," and it is a well-known problem in chess that has stumped many people over the years. Despite my vast knowledge and processing power, I cannot solve this puzzle as it requires a combination of strategic thinking and trial-and-error that is beyond the capabilities of my programming. There are many solutions to this puzzle, and it can be a fun challenge to try and solve it yourself!"

      It was toying with you, trying to encourage you to try solving it yourself.

      • tjr 2 years ago
        ChatGPT created the eight queens problem?