解决数独

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. 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke。

    本文转载:http://blog.csdn.net/playing9c/article/details/7471918 http://blog.csdn.net/beelinkerlidejun/ar ...

  2. Codeforces Round #321 (Div. 2) C. Kefa and Park dfs

    C. Kefa and Park Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/probl ...

  3. Android adt v22.6.2-1085508 自己主动创建 appcompat_v7 解决方法,最低版本号2.2也不会出现

    Android 开发工具升级到22.6.2在创建project时仅仅要选择的最低版本号低于4.0,就会自己主动生成一个项目appcompat_v7,没创建一个新的项目都会自己主动创建,非常是烦恼... ...

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

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

  5. Redirect

    Redirect To use this Class, add the following to the top of the file. use Redirect; Redirect::to($pa ...

  6. ajax的一部分知识

    jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为String类型的参数,请求方式(p ...

  7. Android_sharePreference

    /** * Android的四中数据存储方式: * 1.SharePreferences * 2.SQLite * 3.Content Provider * 4.File * * SharePrefe ...

  8. 用Windows Server 2003搭建企业内部邮件服务器

    公司要搭建一个邮件服务器,方便内部邮件的发送.而且要求每位员工都可以使用自己的账号和密码.领导将这份工作交给我,不过,这可难不倒我.只要借助Windows Server 2003就可以轻松建起内部邮件 ...

  9. HeaderTemplate

    前台代码: <asp:Repeater ID="rptList" runat="server" onitemdatabound="doSomet ...

  10. CountDownLatch(闭锁)

    一.闭锁(Latch)    闭锁(Latch):一种同步方法,可以延迟线程的进度直到线程到达某个终点状态.通俗的讲就是,一个闭锁相当于一扇大门,在大门打开之前所有线程都被阻断,一旦大门打开所有线程都 ...