解决数独

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

思路:

  搜索加剪枝。

  首先设立3个辅助二维数组:rows,  columns, grids来维护目前的数独状态,rows[i][k](1<=i, k<=9)表示第i行是否占用了数字k,同理colums[i][k]表示第i列是否占用了数字k,grid[i][k]表示第i个格子是否占用了数字k,

  然后第一次读入二维数组,更新rows, columns和grids.

  第二次读二维数组,对每个为'.'的元素根据rows,columns和grid来枚举可能的值。如果没有可以枚举的值,直接剪枝返回。

代码如下:

  Runtime: 5 ms

static int columns[][];
static int rows[][];
static int grids[][]; int search(char *board[], int startI, int startJ) { for(int i = startI; i < ; ++i) {
for(int j = i == startI ? startJ : ; j < ; ++j) {
if (board[i][j] == '.') {
for (int k = ; k <= ; ++k) {
if (!rows[i][k] && !columns[j][k] && !grids[i/ + j/ *][k]) {
rows[i][k] = ;
columns[j][k] = ;
grids[i/ + j/ *][k] = ; if (search(board, i, j+) == ) {
board[i][j] = k + '';
return ;
}
rows[i][k] = ;
columns[j][k] = ;
grids[i/ + j/ *][k] = ;
}
}
return ;
}
}
}
return ;
} void solveSudoku(char * board[]) {
memset(columns, , sizeof(columns));
memset(rows, , sizeof(rows));
memset(grids, , sizeof(grids)); for(int i = ; i < ; ++i) {
for(int j = ; j < ; ++j) {
if (board[i][j] != '.') {
rows[i][board[i][j] - ''] = ;
columns[j][board[i][j] - ''] = ; int tmp = i/ + j/ * ;
grids[tmp][board[i][j] - ''] = ;
}
}
}
search(board, , );
}

leetcode problem 37 -- Sudoku Solver的更多相关文章

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

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

  2. 【LeetCode】37. Sudoku Solver

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

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

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

  4. [LeetCode] 37. Sudoku Solver 求解数独

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

  5. Leetcode 笔记 36 - Sudoku Solver

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

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

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

  7. 【LeetCode题意分析&解答】37. Sudoku Solver

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

  8. LeetCode 37 Sudoku Solver(求解数独)

    题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description   Problem : 解决数独问题,给出一个二维数组,将这个数独 ...

  9. Java [leetcode 37]Sudoku Solver

    题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated ...

随机推荐

  1. 从零开始学android开发-详细谈谈intent的startActivityForResult()方法

    1.两种实现activity跳转的方法 实现activity的跳转主要有两种方法,startActivity()和startActivityForResult();例如activity A跳转到act ...

  2. .NET连接MySQL数据库的方法实现

    突然对.NET连接MySQL数据库有点兴趣,于是乎网上到处找资料,学习MySQL的安装,MySQL的使用等等等等,终于搞定了! 最终效果就是显示数据库中数据表的数据: 首先,当然要有MySQL数据库啦 ...

  3. IOS debug网络PonyDebugger 实践篇

    引言:  PonyDebugger是一个很给力的iOS调试工具,它的监视器安装在Chrome浏览器下做为插件使用,通过监视器和PonyDebugger的iOS SDK相辅相成,可以很好的监视App的运 ...

  4. java nio 抛出NonWritableChannelException异常

    抛出异常的代码在此处: MappedByteBuffer buffer = channel.map(MapMode.READ_WRITE, 0, avalible); 其中channel是一个file ...

  5. MyEclipse设置默认的目光格式

    首先,选择菜单 windows-->preference Java-->Code Style-->Code Templates code-->new Java files 然后 ...

  6. pomelo组件..

    1.pomelo会加载lib/components目录下的组件.并设置为属性..和存储在Pomelo.components中..注意这里其实存储的是对象的构造函数.. function load() ...

  7. mysql事务回滚

    首先条件是表要设置为 InnoDB  类型. 当在一个库连接中,通过调用另一个 库名称.表名称,可以回滚: 当用USE dbName后,在两个或多个库操作时,一次只能回滚一个库中的东西: 当在多个数据 ...

  8. 浅谈iOS中的视图优化

    引言: 让我们来思考几个问题,你开发过的产品,它还有可以优化的地方吗?能增加它的帧率吗?能减少多余的CPU计算吗?是不是存在多余的GPU渲染?业务这点工作量对于越来越强大的设备面前显得微不足道,但作为 ...

  9. VIM7.3中文手册

    INDEX *index.txt*     For Vim version 7.3.  最近更新: 2010年7月 VIM 参考手册    by Bram Moolenaar 译者: Willis h ...

  10. 有(无)符号char型及其溢出问题

    转载自:http://blog.sina.com.cn/s/blog_70ec9a6f01014j1h.html 1.char的有无符号类型 char 分为有符号性(signed)和无符号型(unsi ...