Time Limit: 1000MS | Memory Limit: 65536K | |
---|---|---|
Total Submissions: 11304 | Accepted: 4028 |
Description
In the game of Sudoku, you are given a large 9 × 9 grid divided into smaller 3 × 3 subgrids. For example,.2738..1..1…6735…….293.5692.8………..6.1745.364…….9518…7..8..6534.Given some of the numbers in the grid, your goal is to determine the remaining numbers such that the numbers 1 through 9 appear exactly once in (1) each of nine 3 × 3 subgrids, (2) each of the nine rows, and (3) each of the nine columns.
Input
The input test file will contain multiple cases. Each test case consists of a single line containing 81 characters, which represent the 81 squares of the Sudoku grid, given one row at a time. Each character is either a digit (from 1 to 9) or a period (used to indicate an unfilled square). You may assume that each puzzle in the input will have exactly one solution. The end-of-file is denoted by a single line containing the word “end”.
Output
For each test case, print a line representing the completed Sudoku puzzle.
Sample Input
1 | .2738..1..1...6735.......293.5692.8...........6.1745.364.......9518...7..8..6534. |
Sample Output
1 | 527389416819426735436751829375692184194538267268174593643217958951843672782965341 |
解题
大致意思就是说将一个99的数独补充完整 使得每行,每列,每个3 3的九宫格内数字1-9恰好出现一次
如果用朴素的dfs,搜索树很大,一定会炸
可以考虑,如果我们玩数独,会从所有未填的位置里选择 能填的数最少 的位置,而不是认意找一个位置
对于每行,每列,每个九宫格分别用一个9位二进制数保存哪些数字还可以填
对于每个位置,把它所在的行,列,九宫格分别作&运算,就可以得到该位置可以填什么数,使用lowbit运算把能填的数取出
当一个位置填上数后,把行,列,九宫格相应的二进制位改为0,回溯时改回1还原现场
1 | #include <iostream> |