TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

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

1 点作者 davidajackson大约 2 年前
&gt;invent a puzzle you can&#x27;t solve:<p>Here&#x27;s a puzzle that I created that I cannot solve:<p>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.<p>This is known as the &quot;Eight Queens Puzzle,&quot; 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.<p>There are many solutions to this puzzle, and it can be a fun challenge to try and solve it yourself!<p>But:<p>&gt;solve the 8 queens puzzle<p>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.<p>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:<p><pre><code> 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) </code></pre> 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.<p>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.<p>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.

3 条评论

anonymouskimmer大约 2 年前
I can&#x27;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.<p>1 - Without looking up the relevant equations.
ftxbro大约 2 年前
&gt; &quot;This is known as the &quot;Eight Queens Puzzle,&quot; 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!&quot;<p>It was toying with you, trying to encourage you to try solving it yourself.
tjr大约 2 年前
ChatGPT created the eight queens problem?