LeetCode-37-Sudoku Solver, list转字符串join,回溯
来源:互联网 发布:好桌道美化软件 编辑:程序博客网 时间:2024/05/22 15:35
class Solution(object): count=0 ans=[[0 for x in range(9)]for y in range(9)] def solveSudoku(self, board): """ :type board: List[List[str]] :rtype: void Do not return anything, modify board in-place instead. """ B=[[0 for x in range(9)]for y in range(9)] for i in range(9): for j in range(9): if board[i][j]=='.':continue B[i][j]=ord(board[i][j])-ord('0') for i in range(9): for j in range(9): if B[i][j] == 0: flag=self.dfs(B, i, j) if flag: #board=B self.copyBoard(board,B) #print B #print board return break def copyBoard(self,board,B): for i in range(9): for j in range(9): B[i][j]=chr(ord("0")+B[i][j]) for i in range(9): board[i]="".join(B[i]) def dfs(self, B, x, y): for num in range(1, 10): B[x][y] = num if self.judgePoint(B, x, y) == False: B[x][y] = 0 continue nextx = x nexty = y while nextx < 9: nexty = nexty + 1 if nexty == 9: nexty = 0 nextx += 1 if nextx == 9: return True if B[nextx][nexty] == 0: break flag=self.dfs(B, nextx, nexty) if flag: return True B[x][y] = 0 return False def judgePoint(self, board, x, y): area = 0 for i in range(9): if i!=y and board[x][i]==board[x][y]:return False if i!=x and board[i][y]==board[x][y]:return False px = x / 3 * 3 + i / 3 py = y / 3 * 3 + i - i / 3 * 3 c = board[px][py] if c == 0: continue num = c if area & (1 << num) != 0: return False area |= 1 << num return True
阅读全文
1 0
- LeetCode-37-Sudoku Solver, list转字符串join,回溯
- LeetCode Sudoku Solver 回溯
- [LeetCode 37] Sudoku Solver回溯解法
- LeetCode 37 Sudoku Solver 回溯和深度遍历 还要学习
- Leetcode 37 Sudoku Solver
- Leetcode 37: Sudoku solver
- [leetcode 37] Sudoku Solver
- LeetCode(37) Sudoku Solver
- leetcode 37:Sudoku Solver
- leetcode 37: Sudoku Solver
- Leetcode #37 Sudoku Solver
- leetcode-37 Sudoku Solver
- leetcode 37:Sudoku Solver
- LeetCode 37: Sudoku Solver
- LeetCode 37 - Sudoku Solver
- LeetCode(37)-Sudoku Solver
- leetcode 37 Sudoku Solver
- LeetCode 37 Sudoku Solver
- 关于用MyEclipse6.6,jdk1.8,class不编译的问题
- kotlin 中的枚举类和 “when”
- 宽度固定高度按原图比例适应的ImageView
- PAT 1129. Recommendation System (25) 数数
- Test
- LeetCode-37-Sudoku Solver, list转字符串join,回溯
- 第一章 编译程序概述
- 【不想活了】DELPHI 运行程序出现System Error. Code: 5. 拒绝访问。
- Ubuntu 下实现FTP服务器,SSH服务器,邮件服务器,Web服务器(四)
- 欢迎使用CSDN-markdown编辑器
- MySQL索引背后的数据结构及算法原理
- hdu 1069 Humble Numbers LCS变种
- 牛客网-剑指offer-21-栈的压入、弹出序列
- 代理模式基础学习