Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def backtrack(self, row, col):
- if self.map[row][col].type == "v" and self.map[row][col].value[0] == 0:
- for i in range(1,10):
- if (not self.finished) and self.possibleValue(row, col, i):
- self.map[row][col].value[0] = i
- print(str(row) + "," + str(col) + ": ")
- print(self.map[row][col].value[0])
- if col == len(self.map[row]) - 1:
- if row < len(self.map) - 1:
- self.backtrack(row + 1, 0)
- else:
- self.finished = True
- else:
- self.backtrack(row, col + 1)
- if not self.finished:
- self.map[row][col].value[0] = 0
- else:
- if col == len(self.map[row]) - 1:
- if (not self.isFinalRowNonV(row)) or self.possibleValue(row, col, self.map[row][col].value[0]):
- if row < len(self.map) - 1:
- self.backtrack(row + 1, 0)
- else:
- self.finished = True
- else:
- if (not self.isFinalColNonV(row, col)) or self.possibleValue(row, col, self.map[row][col].value[0]):
- self.backtrack(row, col + 1)
- return
- def possibleValue(self, row, col, value):
- possible = True
- i = row-1
- rowValue = value
- while self.map[i][col].type == "v":
- rowValue += self.map[i][col].value[0]
- if self.map[i][col].value[0] == value:
- possible = False
- i -= 1
- if self.map[i][col].type == "b":
- maxRowValue = self.map[i][col].value[0]
- elif self.map[i][col].type == "d":
- maxRowValue = self.map[i][col].value[0]
- if rowValue > maxRowValue or (rowValue != maxRowValue and self.isFinalRowV(row, col)):
- possible = False
- j = col-1
- colValue = value
- while self.map[row][j].type == "v":
- colValue += self.map[row][j].value[0]
- if self.map[row][j].value[0] == value:
- possible = False
- j -= 1
- if self.map[row][j].type == "b":
- maxColValue = self.map[row][j].value[1]
- elif self.map[row][j].type == "r":
- maxColValue = self.map[row][j].value[0]
- if colValue > maxColValue or (colValue != maxColValue and self.isFinalColV(row, col)):
- possible = False
- return possible
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement