题目描述

请编写一个程序,给数独中的剩余的空格填写上数字
空格用字符'.'表示
假设给定的数独只有唯一的解法

这盘数独的解法是:
红色表示填上的解

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.

class Solution {
public:
    void solveSudoku(vector<vector<char>>& board) {
        vector<vector<int>> used1(9,vector<int>(9,0)),used2(9,vector<int>(9,0)),used3(9,vector<int>(9,0));
            fillnum(used1,used2,used3,board);
            solve(used1,used2,used3,board);  
    }
     
    void fillnum(vector<vector<int>> &used1,vector<vector<int>> &used2,vector<vector<int>> &used3,vector<vector<char> > &board) {
        for(int i=0;i<board.size();i++)
            for(int j=0;j<board[0].size();j++)
            if(board[i][j]!='.')
            {
                int num=board[i][j]-'0'-1;
                int k=i/3*3+j/3;
                    used1[i][num]=used2[j][num]=used3[k][num]=1;
            }
    }
      
    bool solve(vector<vector<int>> &used1,vector<vector<int>> &used2,vector<vector<int>> &used3,vector<vector<char>>&board){
       for(int i=0;i<board.size();i++)
            for(int j=0;j<board[0].size();j++){
                int k=i/3*3+j/3;
                if(board[i][j]=='.'){
                    for(int fill=1;fill<10;fill++){
                        if(used1[i][fill-1]==0 && used2[j][fill-1]==0 && used3[k][fill-1]==0){
                        board[i][j]=fill+'0';
                        used1[i][fill-1]=used2[j][fill-1]=used3[k][fill-1]=1;
                        if(solve(used1,used2,used3,board))
                            return true;                            
                        board[i][j]='.';
                        used1[i][fill-1]=used2[j][fill-1]=used3[k][fill-1]=0;
                        }                        
                }   
                    return false;
                }            
            }
        return true;
    }
};

leetcode113:sudoku-solver的更多相关文章

  1. Leetcode 笔记 36 - Sudoku Solver

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

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

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

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

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

  4. 【leetcode】Sudoku Solver

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

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

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

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

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

  7. 【LeetCode】37. Sudoku Solver

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

  8. Valid Sudoku&&Sudoku Solver

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

  9. LeetCode解题报告—— Reverse Nodes in k-Group && Sudoku Solver

    1. Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked list k at a time and ...

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

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

随机推荐

  1. vue显示后端传递的图片流

    一.显示部分(组件我使用的vuetify) <template> <v-container fluid> <v-card width="100%" m ...

  2. Tensorflow学习笔记No.2

    使用函数式API构建神经网络 函数式API相比于keras.Sequential()具有更加灵活多变的特点. 函数式API主要应用于多输入多输出的网络模型. 利用函数式API构建神经网络主要分为3步, ...

  3. URLEncoder.encode编码空格变+号

    今天调用rest接口的时候,使用URLEncoder编码将空格转为了+号,而rest接口方需要将空格转为%20,参照标准 之后用了不少在线的工具测试,有的将空格转为了+号,有的则是转为了%20.看了一 ...

  4. mycat 1.6实现读写分离

    使用mysql的root账号执行mysql>grant all privileges on *.* to mycatuser@% identified by '123456';mysql> ...

  5. 使用git 版本控制的代码在线修调试,如何还原

    在线调试: 先切换成www用户进入项目的根目录比如/data/wwwroot/website su www cd /data/wwwroot/website vi ./api/controllers/ ...

  6. 汕尾6397.7539(薇)xiaojie:汕尾哪里有xiaomei

    汕尾哪里有小姐服务大保健[微信:6397.7539倩儿小妹[汕尾叫小姐服务√o服务微信:6397.7539倩儿小妹[汕尾叫小姐服务][十微信:6397.7539倩儿小妹][汕尾叫小姐包夜服务][十微信 ...

  7. Linux发行版教你如何选 给入门者的选择通法

    Linux的发行版何止琳琅满目,简直是乱入你眼. 本篇将介绍选择发行版的经验和通用法则,主要会从PC角度去谈. 更新于2020年,初次发布于2017年 选择发行版需考虑哪些因素 选择发行版时需要考虑的 ...

  8. STM32中断

    STM32的中断分两个类型:内核异常和外部中断. 内核异常不能够被打断,不能被设置优先级(它的优先级是凌驾于外部中断之上的).常见的内核异常有以下几种:复位(reset),不可屏蔽中断(NMI),硬错 ...

  9. sql server 2008 r2 直接下载地址,可用迅雷下载

    sqlserver 2008 r2 直接下载地址,可用迅雷下载 下载sqlserver 2008 r2 ,微软用了一个下载器,经过从下载器上,将他的地址全部用键盘敲了下来.最终的简体中文版地址如下: ...

  10. 构建调试Linux内核网络的环境Menuos系统

    一.Linux内核源码下载 下载网址为:https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.0.1.tar.xz 下载完成后放入home/Menu ...