如何在数独游戏的prolog列表中设置值(how to set values in a list in prolog for the sudoku game)

编程入门 行业动态 更新时间:2024-10-21 11:48:54
如何在数独游戏的prolog列表中设置值(how to set values in a list in prolog for the sudoku game)

我有这些代码:

:- use_rendering(sudoku). :- use_module(library(clpfd)). sudoku(Rows) :- length(Rows, 9), maplist(same_length(Rows), Rows), append(Rows, Vs), Vs ins 1..9, maplist(all_distinct, Rows), transpose(Rows, Columns), maplist(all_distinct, Columns), Rows = [As,Bs,Cs,Ds,Es,Fs,Gs,Hs,Is], blocks(As, Bs, Cs), blocks(Ds, Es, Fs), blocks(Gs, Hs, Is). blocks([], [], []). blocks([N1,N2,N3|Ns1], [N4,N5,N6|Ns2], [N7,N8,N9|Ns3]) :- all_distinct([N1,N2,N3,N4,N5,N6,N7,N8,N9]), blocks(Ns1, Ns2, Ns3). problem(1, [[_,_,_,_,_,_,_,_,_], [_,_,_,_,_,3,_,8,5], [_,_,1,_,2,_,_,_,_], [_,_,_,5,_,7,_,_,_], [_,_,4,_,_,_,1,_,_], [_,9,_,_,_,_,_,_,_], [5,_,_,_,_,_,_,7,3], [_,_,2,_,1,_,_,_,_], [_,_,_,_,4,_,_,_,9]]). %problem(1, Rows), sudoku(Rows), maplist(portray_clause, Rows).

我想创建一个新的主函数,以[[3,7,2],[5,1,9] ...]的形式接收三元组列表作为输入,这样每个三元组对应一个盒子里面已包含值的网格。 例如,对于上一个列表的情况,[3,7,2]表示第3行第7列中的框包含值2,[5,1,9]表示第5行中的框,第1列,包含值9

这是我个人的学习,谢谢

I have these code:

:- use_rendering(sudoku). :- use_module(library(clpfd)). sudoku(Rows) :- length(Rows, 9), maplist(same_length(Rows), Rows), append(Rows, Vs), Vs ins 1..9, maplist(all_distinct, Rows), transpose(Rows, Columns), maplist(all_distinct, Columns), Rows = [As,Bs,Cs,Ds,Es,Fs,Gs,Hs,Is], blocks(As, Bs, Cs), blocks(Ds, Es, Fs), blocks(Gs, Hs, Is). blocks([], [], []). blocks([N1,N2,N3|Ns1], [N4,N5,N6|Ns2], [N7,N8,N9|Ns3]) :- all_distinct([N1,N2,N3,N4,N5,N6,N7,N8,N9]), blocks(Ns1, Ns2, Ns3). problem(1, [[_,_,_,_,_,_,_,_,_], [_,_,_,_,_,3,_,8,5], [_,_,1,_,2,_,_,_,_], [_,_,_,5,_,7,_,_,_], [_,_,4,_,_,_,1,_,_], [_,9,_,_,_,_,_,_,_], [5,_,_,_,_,_,_,7,3], [_,_,2,_,1,_,_,_,_], [_,_,_,_,4,_,_,_,9]]). %problem(1, Rows), sudoku(Rows), maplist(portray_clause, Rows).

I want to make a new main function that recieves as input a list of triads, in the form [[3,7,2], [5,1,9] ...], such that each triad corresponds to a box inside the grid that already contains a value. For example, for the case of the previous list, [3,7,2] means that the box in row 3, column 7, contains the value of 2, and [5,1,9] indicates that the box in row 5, column 1, contains the value of 9

This is for my personal learning, thank you

最满意答案

我想你只需要一个这样的谓词:

board_value([R,C,V], Board) :- nth1(R, Board, Row), nth1(C, Row, V).

像这样使用它:

?- Board = [[_,_,_,_,_,_,_,_,_], [_,_,_,_,_,3,_,8,5], [_,_,1,_,2,_,_,_,_], [_,_,_,5,_,7,_,_,_], [_,_,4,_,_,_,1,_,_], [_,9,_,_,_,_,_,_,_], [5,_,_,_,_,_,_,7,3], [_,_,2,_,1,_,_,_,_], [_,_,_,_,4,_,_,_,9]], board_value([5,2,1], Board), write(Board). [[_6,_8,_10,_12,_14,_16,_18,_20,_22], [_24,_26,_28,_30,_32,3,_34,8,5], [_36,_38,1,_40,2,_42,_44,_46,_48], [_50,_52,_54,5,_56,7,_58,_60,_62], [_64,1,4,_68,_70,_72,1,_74,_76], [_78,9,_80,_82,_84,_86,_88,_90,_92], [5,_94,_96,_98,_100,_102,_104,7,3], [_106,_108,2,_110,1,_112,_114,_116,_118], [_120,_122,_124,_126,4,_128,_130,_132,9]] Board = [[_6, _8, _10, _12, _14, _16, _18, _20|...], [_24, _26, _28, _30, _32, 3, _34|...], [_36, _38, 1, _40, 2, _42|...], [_50, _52, _54, 5, _56|...], [_64, 1, 4, _68|...], [_78, 9, _80|...], [5, _94|...], [_106|...], [...|...]].

这可能不是很明显,但第5行的第2列现在是1.希望这有帮助!

I think you just need a predicate like this:

board_value([R,C,V], Board) :- nth1(R, Board, Row), nth1(C, Row, V).

Using it like this:

?- Board = [[_,_,_,_,_,_,_,_,_], [_,_,_,_,_,3,_,8,5], [_,_,1,_,2,_,_,_,_], [_,_,_,5,_,7,_,_,_], [_,_,4,_,_,_,1,_,_], [_,9,_,_,_,_,_,_,_], [5,_,_,_,_,_,_,7,3], [_,_,2,_,1,_,_,_,_], [_,_,_,_,4,_,_,_,9]], board_value([5,2,1], Board), write(Board). [[_6,_8,_10,_12,_14,_16,_18,_20,_22], [_24,_26,_28,_30,_32,3,_34,8,5], [_36,_38,1,_40,2,_42,_44,_46,_48], [_50,_52,_54,5,_56,7,_58,_60,_62], [_64,1,4,_68,_70,_72,1,_74,_76], [_78,9,_80,_82,_84,_86,_88,_90,_92], [5,_94,_96,_98,_100,_102,_104,7,3], [_106,_108,2,_110,1,_112,_114,_116,_118], [_120,_122,_124,_126,4,_128,_130,_132,9]] Board = [[_6, _8, _10, _12, _14, _16, _18, _20|...], [_24, _26, _28, _30, _32, 3, _34|...], [_36, _38, 1, _40, 2, _42|...], [_50, _52, _54, 5, _56|...], [_64, 1, 4, _68|...], [_78, 9, _80|...], [5, _94|...], [_106|...], [...|...]].

It may not be obvious, but the 5th row's 2nd column is now 1. Hope this helps!

更多推荐

本文发布于:2023-07-08 20:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1080260.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数独   如何在   列表中   游戏   sudoku

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!