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.

采用回溯法,利用递归实现
如果当前的元素满足数独条件,则继续判断下一个元素。
如果当前的元素不满足数独条件,则返回,递归回溯到上一个元素继续查找
 
由于肯定有解,所以在判断的时候可以不必使用hash表,直接判断其他位置的元素与当前要判断的元素是否相等就可以了。
 
 
 class Solution {
 public:

     bool isValid(vector<vector<char> > &board,int i0,int j0)
     {
         char target=board[i0][j0];

         ;i<;i++)
         {
             if(i==i0) continue;
             if(board[i][j0]==target)
             {
                 return false;
             }
         }

         ;j<;j++)
         {
             if(j==j0) continue;
             if(board[i0][j]==target)
             {
                 return false;
             }
         }

         *;i<i0/*+;i++)
         {

             *;j<j0/*+;j++)
             {
                 if(i==i0&&j==j0) continue;
                 if(board[i][j]==target)
                 {
                     return false;
                 }
             }
         }

         return true;
     }

     bool scanPos(vector<vector<char> > &board,int pos)
     {
         ) return true;

         bool flag=false;
         ;
         ;

         if(board[i0][j0]!='.')
         {
             );
         }

         ;j<=;j++)
         {

             board[i0][j0]='+j;
             if(isValid(board,i0,j0))
             {
                 ))
                 {
                     flag=true;
                     break;
                 }
             }
         }

         if(flag==false)
         {
             board[i0][j0]='.';
             return false;
         }
         else
         {
             return true;
         }
     }

     void solveSudoku(vector<vector<char> > &board) {
         scanPos(board,);
     }
 };

【leetcode】Sudoku Solver的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. 【Leetcode】Pascal&amp;#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  3. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  4. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  5. 【LeetCode】Repeated DNA Sequences 解题报告

    [题目] All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: &quo ...

  6. 【LeetCode】Min Stack 解题报告

    [题目] Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...

  7. 【LeetCode】#7 Reverse Integer

    [Question] Reverse digits of an integer. Example: x = 123, return 321 x = -123, return -321 [My Solu ...

  8. 【LeetCode】#344 Reverse String

    [Question] Write a function that takes a string as input and returns the string reversed. Example: G ...

  9. 【LeetCode】#1 Two Sum

    [Question] Given an array of integers, return indices of the two numbers such that they add up to a ...

随机推荐

  1. android表白app

    一.前言 马上就要520和521了,是不是还有像我一样的单身狗啊.我就知道有,所以这两天简单写了这个小程序(其实是替别人写的),虽然我并不会用去骗女孩子(因为最近太忙了,实习完之后要搞毕设,要搞论文啊 ...

  2. Mysql 常用 SQL 语句集锦 转载(https://gold.xitu.io/post/584e7b298d6d81005456eb53)

    Mysql 常用 SQL 语句集锦 基础篇 //查询时间,友好提示 $sql = "select date_format(create_time, '%Y-%m-%d') as day fr ...

  3. DOM相关知识点以及原型

    DOM(增删改查): 查询 1.获取元素 1.1标准DOM API document.getElementById document.getElementsByTagName document.get ...

  4. hibernate之persistence错误

    错误码: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityM ...

  5. testng.xml文件结构组成及节点属性说明

    TestNG的DTD检查文件:http://testng.org/testng-1.0.dtd.PHP 更多testng配置及说明,请移步http://testdoc.org/docmaster?pi ...

  6. [ACM_图论] Domino Effect (POJ1135 Dijkstra算法 SSSP 单源最短路算法 中等 模板)

    Description Did you know that you can use domino bones for other things besides playing Dominoes? Ta ...

  7. docker安装与启动

    安装docker [root@localhost /]# yum -y install docker-io     更改配置文件 [root@localhost /]# vi /etc/sysconf ...

  8. org.apache.hadoop.conf-Configured

    org.apache.hadoop.conf中的最后一个类,也是这个包中以后用的最频繁的一个,Configurable算是肉体,Configuration算是灵魂吧 package org.apach ...

  9. zTree实现地市县三级级联报错(一)

    zTree实现地市县三级级联 1.详细报错例如以下: java.lang.IllegalStateException: Failed to load ApplicationContext at org ...

  10. I2C转UART

    I2C转UART,51单片机普通IO口模拟I2C从机,解决UART不够的问题 /************************************************************ ...