Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.html
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.算法
A partially filled sudoku which is valid.數組
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.優化
注意到題目中說的,只要當前已經填充的數字是合法的就能夠,不必定要這個數獨是有解.(下面說的九宮格都是指3*3的網格)code
所以只須要判斷9*9網格的每一行、每一列、9個小九宮格是否合法。即若是在每一行、每一列、每一個9個小九宮格內,某個數字重複出現了,當前數獨就是不合法的。 本文地址htm
網上不少解法是:行、列、九宮格、分三個兩重循環來分別判斷是否合法。其實只須要一個兩重循環便可blog
須要注意的是:若是把九宮格按照行從0開始標號,那麼數字board[i][j] 位於第 i/3*3+j/3 個九宮格內遊戲
class Solution { public: bool isValidSudoku(vector<vector<char> > &board) { int rowValid[10] = {0};//用於判斷某一行是否合法,對於行來講這個數組能夠重複使用 int columnValid[9][10] = {0};//用於判斷某一列是否合法 int subBoardValid[9][10] = {0};//用於判斷某一個九宮格是否合法 for(int i = 0; i < 9; i++) { memset(rowValid, 0, sizeof(rowValid)); for(int j = 0; j < 9; j++) if(board[i][j] != '.') { if(!checkValid(rowValid, board[i][j]-'0') || !checkValid(columnValid[j], board[i][j]-'0') || !checkValid(subBoardValid[i/3*3+j/3], board[i][j]-'0')) return false; } } return true; } bool checkValid(int vec[], int val) { if(vec[val] == 1)return false; vec[val] = 1; return true; } };
針對上面的算法,還能夠優化空間。上面的算法中,在雙重循環時,咱們默認了第一重循環表示矩陣的行、第二重循環表示矩陣的列。能夠換一種思路:ip
class Solution { public: bool isValidSudoku(vector<vector<char> > &board) { int rowValid[10] = {0};//用於判斷某一行是否合法 int columnValid[10] = {0};//用於判斷某一列是否合法 int subBoardValid[10] = {0};//用於判斷某一個九宮格是否合法 for(int i = 0; i < 9; i++) { memset(rowValid, 0, sizeof(rowValid)); memset(columnValid, 0, sizeof(columnValid)); memset(subBoardValid, 0, sizeof(subBoardValid)); for(int j = 0; j < 9; j++) { if(!checkValid(rowValid, board[i][j]-'0') || !checkValid(columnValid, board[j][i]-'0') || !checkValid(subBoardValid, board[3*(i/3) + j/3][3*(i%3) + j%3]-'0')) return false; } } return true; } bool checkValid(int vec[], int val) { if(val < 0)return true;//對應的是字符‘.’ if(vec[val] == 1)return false; vec[val] = 1; return true; } };
以上的基礎上,固然咱們還能夠用bitmap來更加壓縮空間leetcode
Write a program to solve a Sudoku puzzle by filling the empty cells.
Empty cells are indicated by the character '.'
.
You may assume that there will be only one unique solution.
A sudoku puzzle...
...and its solution numbers marked in red.
這種類型的遊戲通常回溯法來解決,設置某個空格時,若是該空格不管設置什麼數字都沒法達到合法狀態,那麼回溯從新設置上一個空格,詳細見代碼註釋
class Solution { public: void solveSudoku(vector<vector<char> > &board) { for(int i = 0; i < 9; i++) for(int j = 0; j < 9; j++) if(board[i][j] != '.') fill(i, j, board[i][j] - '0'); solver(board, 0); } bool solver(vector<vector<char> > &board, int index) {// 0 <= index <= 80,index表示接下來要填充第index個格子 if(index > 80)return true; int row = index / 9, col = index - 9*row; if(board[row][col] != '.') return solver(board, index+1); for(int val = '1'; val <= '9'; val++)//每一個爲填充的格子有9種可能的填充數字 { if(isValid(row, col, val-'0')) { board[row][col] = val; fill(row, col, val-'0'); if(solver(board, index+1))return true; clear(row, col, val-'0'); } } board[row][col] = '.';//注意別忘了恢復board狀態 return false; } //判斷在第row行col列填充數字val後,是不是合法的狀態 bool isValid(int row, int col, int val) { if(rowValid[row][val] == 0 && columnValid[col][val] == 0 && subBoardValid[row/3*3+col/3][val] == 0) return true; return false; } //更新填充狀態 void fill(int row, int col, int val) { rowValid[row][val] = 1; columnValid[col][val] = 1; subBoardValid[row/3*3+col/3][val] = 1; } //清除填充狀態 void clear(int row, int col, int val) { rowValid[row][val] = 0; columnValid[col][val] = 0; subBoardValid[row/3*3+col/3][val] = 0; } private: int rowValid[9][10];//rowValid[i][j]表示第i行數字j是否已經使用 int columnValid[9][10];//columnValid[i][j]表示第i列數字j是否已經使用 int subBoardValid[9][10];//subBoardValid[i][j]表示第i個小格子內數字j是否已經使用 };
【版權聲明】轉載請註明出處:http://www.cnblogs.com/TenosDoIt/p/3800485.html