题目内容
补充下列程序(N皇后问题):n = 4cols = [0 for i in range(n)]def checkBoard(rowIndex):for i in range(rowIndex):if cols[i]==cols[rowIndex]:return Falseif abs(cols[i]-cols[rowIndex]) == :return Falsereturn Trueres = []def NQueens(rowIndex):if :board = [[0 for j in range(n)] for i in range(n)]for i in range(n):board[i][cols[i]] = 1res.append(board)returnfor i in range(n):cols[rowIndex] =if checkBoard(rowIndex):NQueens(rowIndex+1)NQueens(0)res
查看答案
搜索结果不匹配?点我反馈