数独求解器性能问题

时间:2018-01-21 22:40:14

标签: performance prolog solver sudoku

我的数据解析器有问题。它的工作正在发挥作用,但表现却很糟糕。对于小的,它工作得很好,但像9x9或更大的更大的需要花费10分钟或更长时间。我想把它保留为像现在这样的无限大小。有人可以帮忙吗?

solve_sudoku(Rows,Sol):-
    length(Rows,Max),
    maplist(same_length(Rows),Rows),
    append(Rows, List), List ins 1..Max,
    maplist(all_distinct, Rows),
    transpose(Rows, Columns),
    maplist(all_distinct,Columns),
    maplist(label,Rows),
    boxes(Boxes,Rows),
    maplist(all_distinct,Boxes),
    boxes_distinct(Boxes),
    Sol = Rows.

boxes(Bs,M) :-
    length(M,Len),
    Sq is round(sqrt(Len)),
    findall(B, (between(1, Sq, R),
            between(1, Sq, C),
            block(M, Sq, R, C, B)), Bs).

cell(M, R,C, V) :-
    nth1(R,M,Row), nth1(C,Row,V).

block(M, Sq, R,C, B) :-
    findall(V, (between(1, Sq, X),
            between(1, Sq, Y),
            I is (R-1) * Sq + X,
            J is (C-1) * Sq + Y,
            cell(M, I, J, V)), B).

boxes_distinct([]).
boxes_distinct([BH|BT]):-
    all_distinct(BH),
    boxes_distinct(BT).

输入是一个列表,其中包含要解决的数据,输出是已解决的数据列表。

2 个答案:

答案 0 :(得分:3)

我认为你应该打电话

maplist(label,Rows)

boxes_distinct(Boxes)

通常你应该在声明所有约束后调用标签或标签。

并使用"用ff或ffc选项标记谓词"而不是"标签" 可能会提高效率。

答案 1 :(得分:2)

boxes_distinct(Boxes)似乎无用,已被maplist(all_distinct,Boxes)覆盖,并在子句末尾附近使用label(List)(而不是maplist(label,Rows))。无论如何,主要问题已由@gabrielte(+1)

指出
相关问题