Leetcode Notoginseng Sudoku Solver (C,c++,java,python)

Source: Internet
Author: User
Tags ord

Problem:

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells is indicated by the character ‘.‘ .

Assume that there would be is only one unique solution.


A Sudoku Puzzle ...


... and its solution numbers marked in red.

Solution: Using the clumsy Dfs method, to determine whether the numbers entered is legal, you can filter out the illegal numbers in advance by three rules, and then try to see if there are any legal numbers left.
Title: Fill Sudoku Game ...

Java source Code (300MS):
public class Solution {public void Solvesudoku (char[][] board) {SudoKu (board,0,0);        } Private Boolean SudoKu (char[][] Board,int I,int j) {if (i==8 && j==9) return true;        if (j==9) {i++;j=0; } if (board[i][j]!= '. ')        {if (SudoKu (board,i,j+1)) return true;            }else{int[] Map=getvalidnum (BOARD,I,J);                    for (int k=1;k<10;k++) {if (map[k]==0) {board[i][j]= (char) (k + ' 0 ');                    if (SudoKu (board,i,j+1)) return true;                Board[i][j]= '. ';    }}} return false;        } private int[] Getvalidnum (char[][] Board,int i,int j) {int[] map=new int[10]; for (int k=0;k<9;k++) {if (board[i][k]!= '. ')            map[board[i][k]-' 0 ']=1; if (board[k][j]!= '. ')        map[board[k][j]-' 0 ']=1; } for (int k=3* (I/3), k<3* (I/3) +3;k++) {for (int l=3* (J/3); l<3* (J/3) +3;l++) {iF (board[k][l]!= '. ')            map[board[k][l]-' 0 ']=1;    }} return map; }}

C Language Source code (12MS):
int* getvalidnum (char** board,int i,int j) {int k,l;    int* map = (int*) malloc (sizeof (int) *10);    memset (map,0,sizeof (int) *10);    for (k=0;k<9;k++) {if (board[i][k]!= '. ') map[board[i][k]-' 0 ']=1;    } for (k=0;k<9;k++) {if (board[k][j]!= '. ') map[board[k][j]-' 0 ']=1; } for (k=3* (I/3), k<3* (I/3) +3;k++) {for (l=3* (J/3); l<3* (J/3) +3;l++) {if (board[k][l]!= '. ') Map[boar        d[k][l]-' 0 ']=1; }} return map;}    BOOL SudoKu (char** board,int i,int j) {int k;    if (i==8 && j==9) return true;        if (j==9) {i++;    j=0; } if (board[i][j]!= '. ')    {if (SudoKu (board,i,j+1)) return true;        }else{int* Map=getvalidnum (board,i,j);                for (k=1;k<10;k++) {if (map[k]==0) {board[i][j]=k+ ' 0 ';                if (SudoKu (board,i,j+1)) return true;            Board[i][j]= '. ';    }} free (map); } return false;} void Solvesudoku (char** board, int boardrowsize, int boaRdcolsize) {SudoKu (board,0,0);} 

C + + source code (24MS):
Class Solution {Public:void Solvesudoku (vector<vector<char>>& board) {SudoKu (board,0,0); }private:bool SudoKu (vector<vector<char>>& board,int i,int j) {if (i==8 && j==9) return T        Rue        if (j==9) {i++;j=0; } if (board[i][j]!= '. ')        {if (SudoKu (board,i,j+1)) return true;            }else{int* Map=getvalidnum (board,i,j);                    for (int k=1;k<10;k++) {if (map[k]==0) {board[i][j]=k+ ' 0 ';                    if (SudoKu (board,i,j+1)) return true;                Board[i][j]= '. ';    }}} return false; } int* getvalidnum (vector<vector<char>>& board,int i,int j) {int* map= (int*) malloc (sizeof (int) * *        0);        memset (map,0,sizeof (int) *10); for (int k=0;k<9;k++) {if (board[i][k]!= '. ')            map[board[i][k]-' 0 ']=1; if (board[k][j]!= '. ')        map[board[k][j]-' 0 ']=1; }       for (int k=3* (I/3), k<3* (I/3) +3;k++) {for (int l=3* (J/3); l<3* (J/3) +3;l++) {if (Board[k] [l]!= '. ')            map[board[k][l]-' 0 ']=1;    }} return map; }};

Python source code (636MS):
Class Solution: # @param {character[][]} board # @return {void} do not return anything, modify board In-place Instea D. def solvesudoku (self, Board): self. SudoKu (board,0,0) def SudoKu (self,board,i,j): If I==8 and J==9:return True if j==9:i+=1;j=0 if boa Rd[i][j]!= '. ': if self.                SudoKu (board,i,j+1): Return True Else:map=self.getvalidnum (board,i,j) for K in range (1,10): If MAP[K]==0:BOARD[I][J]=CHR (K+ord (' 0 ')) if self.        SudoKu (board,i,j+1): Return True board[i][j]= '. '  Return False def getvalidnum (self,board,i,j): Map=[0 for K in range (Ten)] for K in range (9): if        Board[i][k]!= '. ': Map[ord (Board[i][k])-ord (' 0 ')]=1 if board[k][j]!= '. ': Map[ord (Board[k][j])-ord (' 0 ')]=1 For-K in range (I/3), I/3 (+ +3): for-L in range ((J/3), (J/3) +3): If board[k][l]!= '. ': Map[or D (Board[k][L]) -ord (' 0 ')]=1 return map


Leetcode Notoginseng Sudoku Solver (C,c++,java,python)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.