【leetcode】Sudoku Solver

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表,直接判断其他位置的元素与当前要判断的元素是否相等就可以了。

 1 class Solution {
 2 public:
 3
 4     bool isValid(vector<vector<char> > &board,int i0,int j0)
 5     {
 6         char target=board[i0][j0];
 7
 8         for(int i=0;i<9;i++)
 9         {
10             if(i==i0) continue;
11             if(board[i][j0]==target)
12             {
13                 return false;
14             }
15         }
16
17
18         for(int j=0;j<9;j++)
19         {
20             if(j==j0) continue;
21             if(board[i0][j]==target)
22             {
23                 return false;
24             }
25         }
26
27         for(int i=i0/3*3;i<i0/3*3+3;i++)
28         {
29
30             for(int j=j0/3*3;j<j0/3*3+3;j++)
31             {
32                 if(i==i0&&j==j0) continue;
33                 if(board[i][j]==target)
34                 {
35                     return false;
36                 }
37             }
38         }
39
40         return true;
41     }
42
43
44     bool scanPos(vector<vector<char> > &board,int pos)
45     {
46         if(pos==81) return true;
47
48
49         bool flag=false;
50         int i0=pos/9;
51         int j0=pos%9;
52
53         if(board[i0][j0]!=‘.‘)
54         {
55             return scanPos(board,pos+1);
56         }
57
58         for(int j=1;j<=9;j++)
59         {
60
61             board[i0][j0]=‘0‘+j;
62             if(isValid(board,i0,j0))
63             {
64                 if(scanPos(board,pos+1))
65                 {
66                     flag=true;
67                     break;
68                 }
69             }
70         }
71
72         if(flag==false)
73         {
74             board[i0][j0]=‘.‘;
75             return false;
76         }
77         else
78         {
79             return true;
80         }
81     }
82
83
84     void solveSudoku(vector<vector<char> > &board) {
85         scanPos(board,0);
86     }
87 };
时间: 2024-12-12 03:01:02

【leetcode】Sudoku Solver的相关文章

【leetcode】 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. 说明: 数独有

【LeetCode】哈希表 hash_table(共88题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [1]Two Sum (2018年11月9日,k-sum专题,算法群衍生题) 给了一个数组 nums, 和一个 target 数字,要求返回一个下标的 pair, 使得这两个元素相加等于 target . 题解:我这次最大范围的优化代码, hash-table + one pass,时间复杂度 O(N),空间复杂度 O(N).重点在于动态找,一边生成hash-tabl

【LeetCode】回溯法 backtracking(共39题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [10]Regular Expression Matching [17]Letter Combinations of a Phone Number [22]Generate Parentheses (2019年2月13日) 给了一个N,生成N对括号的所有情况的字符串. n = 3 [ "((()))", "(()())", "(

【leetcode刷题笔记】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. 题解:递归.在每个空位

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【LeetCode】Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 这题别想用通项公式做,n choose m里面的连乘必然溢出,老老实实逐层用定义做. class Solution { public: vector<vector<

【LeetCode】Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n