鱼C论坛

 找回密码
 立即注册
查看: 2265|回复: 1

N-Queens 问题求助

[复制链接]
发表于 2017-9-16 05:17:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 yanlizi 于 2017-9-16 05:51 编辑

比较小白,请大家帮忙 感谢!
题目要求: 完成 addOrRemoveConstraints 和 placeMoves

board.py
  1. class Board():
  2. ##########################################
  3. #### Constructor
  4. ##########################################
  5. def __init__(self, n):
  6.         self.n = n
  7.         self.spaces = n * n
  8.         #indicates that all moves are possible at
  9.         #the beginning
  10.         self.constraints = {}
  11.         for i in range(self.spaces):
  12.                 self.constraints[i] = 0
  13.         #holds the final places of the queens
  14.         self.queenSpaces = []

  15. ##########################################
  16. #### Move Functions
  17. ##########################################
  18. #returns all moves that have 0 constraints on them
  19. def getPossibleMoves(self):
  20.         return [move for move in self.constraints if self.constraints[move] == 0]
  21. def makeMove(self, space):

  22. # add the queen
  23.         self.queenSpaces.append(space)
  24. # add the conflicts
  25.         self.addOrRemoveConstraints(space)

  26. def removeMove(self, space):
  27. #remove the queen
  28.         self.queenSpaces.remove(space)
  29. #remove the dependent conflicts
  30.         self.addOrRemoveConstraints(space, add=False)

  31. ##########################################
  32. #### Constraint Logic
  33. ##########################################
  34. #adds or removes constraints along the row, col, and diags of a move
  35. def addOrRemoveConstraints(self, move, add=True):
  36. # choosing whether to use add or remove function
  37. if (add):
  38.         mutationFx = self.addConstraint
  39. else:
  40.         mutationFx = self.removeConstraint
  41. #TODO provide the logic to add or remove constraints from self.constraints
  42. #YOUR CODE HERE
  43. ##########################

  44. #add 1 to the constraint counter for a particular space
  45. def addConstraint(self, move):
  46.         if not move == -1:
  47.                 self.constraints[move] += 1
  48. #remove 1 from the constraint counter for a particular space
  49. def removeConstraint(self, move):
  50.         if not move == -1:
  51.                 self.constraints[move] -= 1
  52. ##########################################
  53. #### Utility Functions
  54. ##########################################
  55. #returns the corresponding space # based on 0-indexed row and column
  56. #returns -1 if the space is not on the board
  57. # e.g.
  58. # rcToSpace(3,4) # the space at row 3, column 4
  59. # > 28 # the corresponding space number given an 8x8 board
  60. def rcToSpace(self, row, col):
  61.         space = row * self.n + col
  62.         if space >= self.spaces or space < 0:
  63.                 return -1
  64.         else:
  65.                 return space

  66. def print(self):
  67.         for r in range(self.n):
  68.                 row = ""
  69.                 for c in range(self.n):
  70.                         if(self.rcToSpace(r,c) in self.queenSpaces):
  71.                                 row += "Q"
  72.                         else:
  73.                                 row += "-"
  74.                         row += " "
  75.         print(row)
复制代码


lab4.py
  1. import board
  2. # board to be manipulated below
  3. b = board.Board(8)
  4. #returns true if there exists a potential queen placement given the current board state
  5. # and leaves b.queenspaces filled with the appropriate queen placements
  6. #false if otherwise
  7. def placeMoves():

  8.   

  9. #Hint 1: This should be recursively defined
  10. #Hint 2: You should only need to use the following three functions from the board class
  11. # - getPossibleMoves
  12. # - makeMove
  13. # - removeMove

  14. if __name__ == "__main__":
  15. placeMoves()
  16. b.print()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-9-16 08:10:43 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-23 23:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表