利用短整数类型和部分字符串优化DuckDB利用数组求解数独SQL

与利用定长字符串版本相比,利用整数数组的速度略慢,这是因为数组是复杂数据结构,有开销,这个开销抵消了字符串转整数的开销。

因此,它还有几处细节可以优化。

1.存储二进制位状态的rows, cols, boxes数组都保存了大整数,而在每个递归步骤都要保存当前的状态,多用了几倍的空间,考虑每个二进制不超过512,可以用转成smallintl类型来优化。

实测计算只有17个已知数的最小数独,定长字符串版本用时3.048秒,大整数数组用时4.686 秒,短整数数组用时3.212秒。

2.每步用instr从字符串找最前.位置不必要,其实每步的.位置都是确定的, 可以在第一步计算完毕,供以后读取。

3.基于第二点,每步保存的字符串不必是完整字符串,只要保存当前状态部分就够了,然后在最后一步,将它与input再合并成完整字符串。使用了这个优化,短整数数组用时2.881秒,终于反超了定长字符串版本。

sql 复制代码
WITH RECURSIVE
  input(sud) AS (
 VALUES('9......4..5..2........1......69..1........5.24..7......1....3.....6...9....4.....')
--    VALUES('.....1..7....6..2.8..9..3...954....3..3...4..4......8......7..6.1..2....5..3..9..')
  ),
  a(ins) as(select list(i::tinyint order by i) from input, range(1, 82)t(i) where sud[i]='.'), 
  digits(z, lp, bit) AS (
    VALUES('1', 1, 1::smallint)
    UNION ALL SELECT 
    CAST(lp+1 AS TEXT), lp+1, bit * 2 FROM digits WHERE lp<9
  ),
  -- 初始化行列宫的二进制状态(基于已有数字)
  init_state AS (
    SELECT 
      sud,(select list(s::smallint order by rn) from(select sum(bit)s,rn from(select bit,i//9 rn from digits, range(0,81) t(i) where substr(sud,i+1,1)=z union all select 0,i from range(9)t(i)) group by rn)) as rows,

      -- 列的二进制状态数组
      (select list(s::smallint order by rn) from(select sum(bit)s,rn from(select bit,i%9 rn from digits, range(0,81) t(i) where substr(sud,i+1,1)=z union all select 0,i from range(9)t(i)) group by rn)) as cols,

      -- 宫的二进制状态数组
      (select list(s::smallint order by rn) from(select sum(bit)s,rn from(select bit,(i//27*3 + i%9//3) rn from digits, range(0,81) t(i) where substr(sud,i+1,1)=z union all select 0,i from range(9)t(i)) group by rn)) as boxes
    FROM input
  ),
  -- 递归求解
  x(lv, s, ind, rows, cols, boxes) AS (
    SELECT 1::tinyint, '', 
      --substr(replace(sud,'.',''),1,ins[1]-1) 
      ins[1], 
      --instr(sud, '.'),
      rows,
      cols,
      boxes
    FROM init_state, a
    UNION ALL
    SELECT lv+1, 
      s||z, 
      --substr(s, 1, ind-1) || z || substr(s, ind+1),
      ins[lv+1], 
      --instr( substr(s, 1, ind-1) || z || substr(s, ind+1), '.' ),
      -- 更新行的二进制状态
      list_transform(rows, (val, idx) -> 
        CASE WHEN idx = ((ind-1)//9) + 1 THEN val | z.bit ELSE val END),
      -- 更新列的二进制状态
      list_transform(cols, (val, idx) -> 
        CASE WHEN idx = ((ind-1)%9) + 1 THEN val | z.bit ELSE val END),
      -- 更新宫的二进制状态
      list_transform(boxes, (val, idx) -> 
        CASE WHEN idx = (((ind-1)//27)*3 + ((ind-1)%9)//3) + 1 THEN val | z.bit ELSE val END)
    FROM x, digits AS z, a
    WHERE ind>0
      -- AND z.lp BETWEEN 1 AND 9
      -- 使用位运算检查是否可以放置数字
      AND (rows[((ind-1)//9) + 1] & z.bit) = 0
      AND (cols[((ind-1)%9) + 1] & z.bit) = 0
      AND (boxes[(((ind-1)//27)*3 + ((ind-1)%9)//3) + 1] & z.bit) = 0
 )--select s from x,a where lv=len(ins)+1;
  , j as(select s from x,a where lv=len(ins)+1) --结果部分字符串
 , j2 as(select substr(s, i, 1) s, ins[i] idx from j, a , range(1, 82)t(i) where i <=len(ins)) --带位置的结果字符表
 , j3 as(select substr(sud, i, 1)s, i idx from input , range(1, 82)t(i) where substr(sud, i, 1)<>'.') --带位置的初始字符表
 select listagg(s, '' order by idx) from (from j2 union all from j3);
相关推荐
权泽谦2 小时前
从零搭建一个 PHP 登录注册系统(含完整源码)
android·开发语言·php
PieroPc2 小时前
用python Streamlit 做个RapidOCR 文本识别系统
开发语言·python·ocr
暖木生晖2 小时前
Javascript函数之匿名函数以及立即执行函数的使用方法?
开发语言·javascript·ecmascript
一 乐2 小时前
医疗管理|医院医疗管理系统|基于springboot+vue医疗管理系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·医疗管理系统
say_fall2 小时前
C语言容易被忽略的易错点(2)
c语言·开发语言
syker3 小时前
NEWBASIC 2.06.7 API 帮助与用户使用手册
开发语言·人工智能·机器学习·自动化
Js_cold3 小时前
Verilog运算符
开发语言·fpga开发·verilog
努力还债的学术吗喽3 小时前
【项目】pyqt5基于python的照片整蛊项目
开发语言·python·qt
m0_569531013 小时前
shell(4)--shell脚本中的循环:(if循环,for,while,until)和退出循环(continue,break, exit)
开发语言