import numpy as np
import sys
sys.setrecursionlimit(1000) #例如这里设置为一百万

def get1(n):
    if n<3:
        return 0
    if n<6:
        return 3
    return 6

def get2(n):
    if n<3:
        return 3
    if n<6:
        return 6
    return 9

def get3(arr,i,j):
    a1=list(arr[i,:])
    a2=list(arr[:,j])
    a3=list(arr[get1(i):get2(i),get1(j):get2(j)].reshape(1,-1)[0])
    a=set(a1+a2+a3)
    return b-a

def sd(arr,dep):
    count[0]+=1
    if dep==len(l):
        result.append(arr)
    else:
        x,y=l[dep]
        lac=get3(arr,x,y)
        if len(lac)>0 and len(result)==0:        
            for i in lac:
                ac=arr.copy()
                ac[x,y]=i
                sd(ac,dep+1)
    
    
array= [[5,3,0,0,7,0,0,0,0],
        [6,0,0,1,9,5,0,0,0],
        [0,9,8,0,0,0,0,6,0],
        [8,0,0,0,6,0,0,0,3],
        [4,0,0,8,0,3,0,0,1],
        [7,0,0,0,2,0,0,0,6],
        [0,6,0,0,0,0,2,8,0],
        [0,0,0,4,1,9,0,0,5],
        [0,0,0,0,8,0,0,7,9]]
array=np.array(array)
b,l,old,result,count={1,2,3,4,5,6,7,8,9},[],[],[],[0]
for i in range(0,9):
    for j in range(0,9):
        if array[i,j]==0:
            l.append((i,j))
sd(array,0)
print(result)

leetcode python 037 求解数独的更多相关文章

  1. [LeetCode] Sudoku Solver 求解数独

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

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

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

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

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

  4. Leetcode Python Solution(continue update)

    leetcode python solution 1. two sum (easy) Given an array of integers, return indices of the two num ...

  5. 求解数独难题, Sudoku问题(回溯)

    Introduction : 标准的数独游戏是在一个 9 X 9 的棋盘上填写 1 – 9 这 9 个数字,规则是这样的: 棋盘分成上图所示的 9 个区域(不同颜色做背景标出,每个区域是 3 X 3 ...

  6. 算法实践——舞蹈链(Dancing Links)算法求解数独

    在“跳跃的舞者,舞蹈链(Dancing Links)算法——求解精确覆盖问题”一文中介绍了舞蹈链(Dancing Links)算法求解精确覆盖问题. 本文介绍该算法的实际运用,利用舞蹈链(Dancin ...

  7. 关于用舞蹈链DLX算法求解数独的解析

    欢迎访问——该文出处-博客园-zhouzhendong 去博客园看该文章--传送门 描述 在做DLX算法题中,经常会做到数独类型的题目,那么,如何求解数独类型的题目?其实,学了数独的构建方法,那么DL ...

  8. 转载 - 算法实践——舞蹈链(Dancing Links)算法求解数独

    出处:http://www.cnblogs.com/grenet/p/3163550.html 在“跳跃的舞者,舞蹈链(Dancing Links)算法——求解精确覆盖问题”一文中介绍了舞蹈链(Dan ...

  9. 前端与算法 leetcode 36. 有效的数独

    目录 # 前端与算法 leetcode 36. 有效的数独 题目描述 概要 提示 解析 算法 传入[['5', '3', '.', '.', '7', '.', '.', '.', '.'],['6' ...

随机推荐

  1. 论文阅读:Siam-RPN

    摘要 Siam-RPN提出了一种基于RPN的孪生网络结构.由孪生子网络和RPN网络组成,它抛弃了传统的多尺度测试和在线跟踪,从而使得跟踪速度非常快.在VOT实时跟踪挑战上达到了最好的效果,速度最高16 ...

  2. 深度学习环境搭建(ubuntu16.04+Titan Xp安装显卡驱动+Cuda9.0+cudnn+其他软件)

    一.硬件环境 ubuntu 16.04LTS + windows10 双系统 NVIDIA TiTan XP 显卡(12G) 二.软件环境 搜狗输入法 下载地址 显卡驱动:LINUX X64 (AMD ...

  3. git checkout branch

    git fetch origin feature/banch1:feature/banch1 git checkout feature/banch1 git branch -u origin/feat ...

  4. Python自学:第三章 使用del语句删除元素

    motorcycles = ["honda", "yamaha", "suzuki"] print(motorcycles) del mot ...

  5. python 装饰器问题及解决方式

    #统计函数运行时间 import time #装饰器 def timmer(func): def wrapper(*args,**kwargs): start_time=time.time() fun ...

  6. 思科模拟器PacketTracer7--利用一台交换机将两台pc划分到不同vlan下

    实验2—3 实验内容:将同一交换机下的两台pc划分到不同vlan中 实验工具:思科模拟器PacketTracer7 使用设备:一台交换机,两台PC 实验步骤: 一.配置网络拓扑图 注:1.连线可选择闪 ...

  7. linux上docker安装centos7.2

    1.安装 docker pull centos:7.2.1511 2.启动镜像 docker run -d -i -t <IMAGE ID> /bin/bash 3.进入容器 docker ...

  8. docker容器自动化部署(一)

    1.docker容器暴露多个端口 To expose just one port, this is what you need to do: docker run -p <host_port&g ...

  9. 解决反射型XSS漏洞攻击

    对于程序员来说安全防御,无非从两个方面考虑,要么前端要么后台. 一.首先从前端考虑过滤一些非法字符. 前端的主控js中,在<textarea> 输入框标签中,找到点击发送按钮后,追加到聊天 ...

  10. 移动端input输入placeholder垂直不居中

    在移动端编写input输入框时候,为了输入文字与输入框垂直居中,一般情况下,会将input的line-height的高度等于height.但在移动端输入的时候会发现,虽然输入内容确实是垂直居中了,但是 ...