Sudoku

Inefficient Print All

public static void main(String[] args)
        {
         solveSudoku(new char[N][N]);

        }
     static int N =9; //4
     static int s = 3;  //2
     static boolean solved = false;
     public static void solveSudoku(char[][] board){
            solveSudoku(board, 0, 0);
        }

        private static void solveSudoku(char[][] board, int rowFrom, int colFrom) {

            for(int r = rowFrom; r<N; ++r)
            {
                for(int c = colFrom; c<N; ++c)
                {
                    int n = 1;
                    while(board[r][c]==‘\0‘ && n<=N)
                    {
                        if(isValid(board, r, c, Character.forDigit(n, 10)))
                        {
                            board[r][c] = Character.forDigit(n, 10);
                            solveSudoku(board, r, c+1);
                            if(solved)  //comment this out to get all sudoku
                                return;
                            board[r][c] = ‘\0‘;
                        }
                        ++n;
                    }
                    if(n == N+1)
                        return;
                }
                colFrom = 0;
            }

            solved = true;
            for(int i = 0; i< N; i++)
            {
                for(int j = 0; j< N; j++)
                    System.out.print(board[i][j] + ",");
                System.out.println();
            }
            System.out.println("==============");
        }

        private static boolean isValid(char[][] board, int row, int col, char val)
        {
            for(int c = 0; c<N; ++c)
                if(board[row][c] == val)
                    return false;

            for(int r = 0; r<N; ++r)
                if(board[r][col] == val)
                    return false;

            int rr = row/s;
            int cc = col/s;
            for(int r = rr*s; r<rr*s+s; ++r)
                for(int c = cc*s; c<cc*s+s; ++c)
                    if(board[r][c] == val)
                        return false;

            return true;
        }
时间: 2024-10-29 10:45:56

Sudoku的相关文章

LeetCode37 Sudoku Solver

题目: 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.  (Hard)

*Sudoku Solver

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. public clas

Valid Sudoku

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. 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

[LeetCode]Valid Sudoku

检测数独是否合格. 思路: 填充一遍就知道是否合格. 基本暴力搜索的思想. 1 /*************************************************************************************************** 2 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. 3 The Sudoku board could be parti

POJ Sudoku 数独填数 DFS

题目链接:Sudoku Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18105   Accepted: 8772   Special Judge Description Sudoku is a very simple task. A square table with 9 rows and 9 columns is divided to 9 smaller squares 3x3 as shown on the Fig

Valid Sudoku leetcode

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. 判断九宫格的合理性(并不一定有解),只需要依次判断行.列.9个子九宫格是否

LeetCode OJ:Valid Sudoku(有效数独问题)

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. 注意这里的有效数独并非指的是可以解出来,只要存在的数满足数独的条件就可以了. 原理很简单,但是判定在同一个blocks的时候出了点问题,没想到判定方法,看了下

Java [leetcode 37]Sudoku Solver

题目描述: 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. 解题思路:

2015南阳CCPC H - Sudoku 暴力

H - Sudoku Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 无 Description Yi Sima was one of the best counselors of Cao Cao. He likes to play a funny game himself. It looks like the modern Sudoku, but smaller. Actually, Yi Sima was playing it different. F

leetcode笔记:Valid Sudoku

一.题目描述 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules: http://sudoku.com.au/TheRules.aspx . The Sudoku board could be partially filled, where empty cells are filled with the character '.'. The following figure: A partially f