//C++
/**
* Sudoku Solver
* 解数独
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <iostream>
#include <vector>
using namespace std;
const int SodukuSize = 9;
bool row_mask[SodukuSize][SodukuSize];
bool col_mask[SodukuSize][SodukuSize];
bool area_mask[SodukuSize][SodukuSize];
bool initSudokuMask(vector< vector<char> > &board)
{
//reset the memory
memset(row_mask, false, sizeof(row_mask));
memset(col_mask, false, sizeof(col_mask));
memset(area_mask, false, sizeof(area_mask));
//check each rows and cols
for(int r=0; r<board.size(); r++)
{
for (int c=0; c<board[r].size(); c++)
{
if (!isdigit(board[r][c]))
{
continue;
};
int idx = board[r][c] - '0' - 1;
//check the rows/cols/areas
int area = (r/3) * 3 + (c/3);
if (row_mask[r][idx] || col_mask[c][idx] || area_mask[area][idx] )
{
return false;
}
row_mask[r][idx] = col_mask[c][idx] = area_mask[area][idx] = true;
}
}
return true;
}
bool recursiveSudoKu(vector< vector<char> > &board, int row, int col)
{
if (row >= SodukuSize)
{
return true;
}
if (col >= SodukuSize)
{
return recursiveSudoKu(board, row+1, 0);
}
if (board[row][col] != '.')
{
return recursiveSudoKu(board, row, col+1);
}
//pick a number for empty cell
int area;
for(int i=0; i<SodukuSize; i++)
{
area = (row/3) * 3 + (col/3);
if (row_mask[row][i] || col_mask[col][i] || area_mask[area][i] )
{
continue;
}
//set the number and sovle it recursively
board[row][col] = i + '1';
row_mask[row][i] = col_mask[col][i] = area_mask[area][i] = true;
if (recursiveSudoKu(board, row, col+1) == true)
{
return true;
}
//backtrace
board[row][col] = '.';
row_mask[row][i] = col_mask[col][i] = area_mask[area][i] = false;
}
return false;
}
void solveSudoku(vector<vector<char> > &board)
{
if (initSudokuMask(board) == false)
{
return;
}
recursiveSudoKu(board, 0, 0);
}
int main(int argc, char**argv)
{
return 0;
}
#Python
/**
* Sudoku Solver
* 解数独
*/
class Solution(object):
def solveSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: void Do not return anything, modify board in-place instead.
"""
cacheBox = [[0] * len(board) for _ in range(len(board))]
cacheRow = [[0] * len(board) for _ in range(len(board))]
cacheCol = [[0] * len(board) for _ in range(len(board))]
def helper(board, i, j, cacheRow, cacheCol, cacheBox):
if board[i][j] == ".":
for k in range(1, 10):
if i < 0 or i >= len(board) or j < 0 or j >= len(board):
continue
ib = (i/3) * 3 + j / 3
if cacheRow[i][k - 1] == 1 or cacheCol[j][k - 1] == 1or cacheBox[ib][k - 1] == 1:
continue
cacheRow[i][k - 1] = cacheCol[j][k - 1] = cacheBox[ib][k - 1] = 1
board[i][j] = str(k)
if i == j == len(board) - 1:
return True
if i + 1 < len(board):
if helper(board, i + 1, j, cacheRow, cacheCol, cacheBox):
return True
elif j + 1 < len(board):
if helper(board, 0, j + 1, cacheRow, cacheCol, cacheBox):
return True
board[i][j] = "."
cacheRow[i][k - 1] = cacheCol[j][k - 1] = cacheBox[ib][k - 1] = 0
else:
if i == j == len(board) - 1:
return True
if i + 1 < len(board):
if helper(board, i + 1, j, cacheRow, cacheCol, cacheBox):
return True
elif j + 1 < len(board):
if helper(board, 0, j + 1, cacheRow, cacheCol, cacheBox):
return True
return False
for i in range(len(board)):
for j in range(len(board)):
if board[i][j] != ".":
ib = (i/3) * 3 + j / 3
k = int(board[i][j]) - 1
cacheRow[i][k] = cacheCol[j][k] = cacheBox[ib][k] = 1
print helper(board, 0, 0, cacheRow, cacheCol, cacheBox)