|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 yanlizi 于 2017-9-16 05:51 编辑
比较小白,请大家帮忙 感谢!
题目要求: 完成 addOrRemoveConstraints 和 placeMoves
board.py
- class Board():
- ##########################################
- #### Constructor
- ##########################################
- def __init__(self, n):
- self.n = n
- self.spaces = n * n
- #indicates that all moves are possible at
- #the beginning
- self.constraints = {}
- for i in range(self.spaces):
- self.constraints[i] = 0
- #holds the final places of the queens
- self.queenSpaces = []
- ##########################################
- #### Move Functions
- ##########################################
- #returns all moves that have 0 constraints on them
- def getPossibleMoves(self):
- return [move for move in self.constraints if self.constraints[move] == 0]
- def makeMove(self, space):
- # add the queen
- self.queenSpaces.append(space)
- # add the conflicts
- self.addOrRemoveConstraints(space)
- def removeMove(self, space):
- #remove the queen
- self.queenSpaces.remove(space)
- #remove the dependent conflicts
- self.addOrRemoveConstraints(space, add=False)
- ##########################################
- #### Constraint Logic
- ##########################################
- #adds or removes constraints along the row, col, and diags of a move
- def addOrRemoveConstraints(self, move, add=True):
- # choosing whether to use add or remove function
- if (add):
- mutationFx = self.addConstraint
- else:
- mutationFx = self.removeConstraint
- #TODO provide the logic to add or remove constraints from self.constraints
- #YOUR CODE HERE
- ##########################
- #add 1 to the constraint counter for a particular space
- def addConstraint(self, move):
- if not move == -1:
- self.constraints[move] += 1
- #remove 1 from the constraint counter for a particular space
- def removeConstraint(self, move):
- if not move == -1:
- self.constraints[move] -= 1
- ##########################################
- #### Utility Functions
- ##########################################
- #returns the corresponding space # based on 0-indexed row and column
- #returns -1 if the space is not on the board
- # e.g.
- # rcToSpace(3,4) # the space at row 3, column 4
- # > 28 # the corresponding space number given an 8x8 board
- def rcToSpace(self, row, col):
- space = row * self.n + col
- if space >= self.spaces or space < 0:
- return -1
- else:
- return space
- def print(self):
- for r in range(self.n):
- row = ""
- for c in range(self.n):
- if(self.rcToSpace(r,c) in self.queenSpaces):
- row += "Q"
- else:
- row += "-"
- row += " "
- print(row)
复制代码
lab4.py
- import board
- # board to be manipulated below
- b = board.Board(8)
- #returns true if there exists a potential queen placement given the current board state
- # and leaves b.queenspaces filled with the appropriate queen placements
- #false if otherwise
- def placeMoves():
-
- #Hint 1: This should be recursively defined
- #Hint 2: You should only need to use the following three functions from the board class
- # - getPossibleMoves
- # - makeMove
- # - removeMove
- if __name__ == "__main__":
- placeMoves()
- b.print()
复制代码 |
|