题目

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.

题解:

第一反应就是N皇后问题。就是一点点尝试着填数,不行的话就回溯,直到都填满就返回。

如果对一个格子尝试从0~9都不行,那么说明整个sudoku无解,返回false就好。

对整个棋盘所有'.'都填完了,那么就可以返回true了。

 1     public void solveSudoku(char[][] board) {

 2         if (board==null||board.length==0)

 3             return;

 4         helper(board);

 5     }

 6     

 7     private boolean helper(char[][] board){

 8         for(int i=0; i<board.length; i++){

 9             for (int j=0; j<board[0].length; j++){

                 if (board[i][j]=='.'){

                     for (char num='1'; num<='9'; num++){//尝试

                         if(isValid(board, i, j, num)){

                             board[i][j]=num;

                             

                             if (helper(board))

                                 return true;

                             else

                                 board[i][j]='.';//回退

                         }

                     }

                     return false;

                 }

             }

         }

         

         return true;

     }

     

     private boolean isValid(char[][] board, int i, int j, char c){

         // check column

         for (int row=0; row<9; row++)

             if (board[row][j] == c)

                 return false;

         

        // check row

         for (int col=0; col<9; col++)

             if (board[i][col]==c)

                 return false;

       

         // check block

         for(int row=i/3*3; row<i/3*3+3; row++)

             for (int col=j/3*3; col<j/3*3+3; col++)

                 if (board[row][col]==c)

                     return false;

                     

         return true;

     }

Reference:http://rleetcode.blogspot.com/2014/01/sudoku-solver-java.html

Sudoku Solver leetcode java的更多相关文章

  1. Sudoku Solver [LeetCode]

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  2. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  3. [Leetcode][Python]37: Sudoku Solver

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...

  4. Leetcode之回溯法专题-37. 解数独(Sudoku Solver)

    Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...

  5. LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)

    Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...

  6. [leetcode]算法题目 - Sudoku Solver

    最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...

  7. 【leetcode】Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

  8. leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题

    三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...

  9. 【LeetCode】37. Sudoku Solver

    Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...

随机推荐

  1. bzoj4289 Tax

    Description 给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价.起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边 ...

  2. JVM启动流程

    JVM启动流程 (1)在java中jvm是通过java或javaw命令启动的,后面跟加载的类名. (2)jvm在启动的时候先根据[当前路径和系统版本寻找jvm的配置文件jvm.cfg]装载配置. (3 ...

  3. HDU 3974 Assign the task(简单线段树)

    Assign the task Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. Delphi模拟最小化恢复关闭按纽

    https://yq.aliyun.com/wenji/96083 本文讲的是Delphi模拟最小化恢复关闭按纽, 我们做多文档应用程序开发时,如果在主From中指定mainMenu时,在主菜单上右角 ...

  5. delphi 消息的使用

    //分析结果 WM_AnalysisResult = WM_USER + 1009; SendMessage(G_MainHandle, WM_AnalysisResult, 0, 0); proce ...

  6. 在qemu模拟的aarch32上使用kgtp

    KGTP 介绍 KGTP 是一个能在产品系统上实时分析 Linux 内核和应用程序(包括 Android)问题的全面动态跟踪器. 使用 KGTP 不需要 在 Linux 内核上打 PATCH 或者重新 ...

  7. Xcode工程文件打不开:cannot be opened because the project file cannot be parsed

    svn更新代码后,打开xcode工程文件,会出现  xxx..xcodeproj  cannot be opened because the project file cannot be parsed ...

  8. C#编程(三十一)----------泛型总结

    C#泛型总结 C#中的所谓的泛型程序设计和C++中相应的模版类似. 泛型方法 C#中的泛型方法是指使用了类型参数的方法成员,案例: static void Main(string[] args) { ...

  9. AIDL interface XXX should be declared in a file

    在写AIDL的时候出现了interface XXX should be declared in a file, 错误...经过反复查看,发现AIDL规定,文件名必须和interface XXX名字相同 ...

  10. Spring data jpa Specification查询关于日期的范围搜索

    代码: 时间格式化类型: SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat s ...