leetcode113:sudoku-solver
题目描述

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的更多相关文章
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- LeetCode:Valid Sudoku,Sudoku Solver(数独游戏)
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- [leetcode]算法题目 - Sudoku Solver
最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后 ...
- 【leetcode】Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- [Leetcode][Python]37: Sudoku Solver
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 37: Sudoku Solverhttps://oj.leetcode.co ...
- leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku ...
- 【LeetCode】37. Sudoku Solver
Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are i ...
- Valid Sudoku&&Sudoku Solver
Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku bo ...
- 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 ...
- Leetcode之回溯法专题-37. 解数独(Sudoku Solver)
Leetcode之回溯法专题-37. 解数独(Sudoku Solver) 编写一个程序,通过已填充的空格来解决数独问题. 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次.数字 1 ...
随机推荐
- .NET Standard 简介
系列目录 [已更新最新开发文章,点击查看详细] .NET Standard 是一套正式的 .NET API 规范,有望在所有 .NET 实现中推出. 推出 .NET Standard 的背后动 ...
- AMD Ryzen 5000系列桌面处理器 2020年10月8日发布
AMD Ryzen 5 5600X 6核心12线程,基础频率3.7GHz,最大频率4.6GHz,二级缓存3MB,三级缓存32MB,不锁频,支持DDR4 3200MHz内存,台积电7纳米工艺,PCIe ...
- JVM(五):JVM模型与GC
确定垃圾 引用计数(存在循环引用问题) 根可达算法 常见的垃圾回收算法 标记清除算法-位置不连续,产生碎片 拷贝算法- 没有碎片,浪费空间 标记压缩-没有碎片,效率偏低(多线程需要进行线程同步,单线程 ...
- JVM系列【6】GC与调优1
JVM系列笔记目录 虚拟机的基础概念 class文件结构 class文件加载过程 jvm内存模型 JVM常用指令 GC与调优 GC基础知识 什么是垃圾 没有任何引用指向的一个对象或多个对象(循环引 ...
- vs code个性化设置
文件=>首选项=>设置,直接在搜索栏搜索有背景色的部分即可 1. 鼠标滚轮缩放 "editor.mouseWheelZoom": true 2. 显示空格和tab符号 ...
- 多路查找树(2-3 树、2-3-4 树、B 树、B+ 树)
本文参考自<大话数据结构> 计算机中数据的存储 一般而言,我们都是在内存中处理数据,但假如我们要操作的数据集非常大,内存无法处理了,在这种情况下对数据的处理需要不断地从硬盘等存储设备中调入 ...
- spring boot:用redis+lua实现基于ip地址的分布式流量限制(限流/简单计数器算法)(spring boot 2.2.0)
一,限流有哪些环节? 1,为什么要限流? 目的:通过对并发请求进行限速或者一个时间单位内的的请求进行限速,目的是保护系统可正常提供服务,避免被压力太大无法响应服务. 如果达到限制速率则可以采取预定的处 ...
- CentOS7防止root密码被破解
破解root密码 为了防止服务器被破坏,为了守护业务的和平,在服务器安全方面,首先我们要做到密码的安全.那么知道如何破解root密码才能让我们有针对性的防护.另外如果我们忘掉了root密码,也能知道如 ...
- java应用启动报错Unable to access jarfile xxxxx.jar
当使用命令:javar -jar xxxx.jar 启动应用时,报错Unable to access jarfile xxxxx.jar,这种主要是 jar 的名称或者路径有问题:
- 【原创】linux实时操作系统xenomai x86平台基准测试(benchmark)
一.前言 benchmark 即基准测试.通常操作系统主要服务于应用程序,其运行也是需要一定cpu资源的,一般来说操作系统提供服务一定要快,否则会影响应用程序的运行效率,尤其是实时操作系统.所以本文针 ...