Prolog第三天作業

2021-09-30 13:02:24 字數 2016 閱讀 5518

1.解決6x6 與9x9數獨問題。

擴充套件原例子4x4的數獨,重複寫行、列、格仔太麻煩了,直接貼4x4的例子:

:- use_module(library(clpfd)).

valid().

valid([head|tail]) :-

all_different(head),

valid(tail).

sudoku(puzzle, solution) :-

solution = puzzle,

puzzle = [s11, s12, s13, s14,

s21, s22, s23, s24,

s31, s32, s33, s34,

s41, s42, s43, s44],

puzzle ins 1..4,

row1 = [s11, s12, s13, s14],

row2 = [s21, s22, s23, s24],

row3 = [s31, s32, s33, s34],

row4 = [s41, s42, s43, s44],

col1 = [s11, s21, s31, s41],

col2 = [s12, s22, s32, s42],

col3 = [s13, s23, s33, s43],

col4 = [s14, s24, s34, s44],

square1 = [s11, s12, s21, s22],

square2 = [s13, s14, s23, s24],

square3 = [s31, s32, s41, s42],

square4 = [s33, s34, s43, s44],

valid([row1, row2, row3, row4,

col1, col2, col3, col4,

square1, square2, square3, square4]),

writeln(row1),

writeln(row2),

writeln(row3),

writeln(row4).

2.採用乙個皇后列表的方式解決八皇后問題,使用1~8範圍內的數字代表每個皇后,通過皇后列表中的位置取其行號並通過其在列表的值取其列號。

ps.不知道怎麼取表中位置,用index代替下表

:- use_module(library(clpfd)).

valid_col().

valid_col([col|tail]) :-

member(col, [1, 2, 3, 4, 5, 6, 7, 8]),

valid_col(tail).

diags1(, , ).

diags1([row|rowstail], [col|colstail], [diagonal|diagonaltail]) :-

diagonal is row - col,

diags1(rowstail, colstail, diagonaltail).

diags2(, , ).

diags2([row|rowstail], [col|colstail], [diagonal|diagonaltail]) :-

diagonal is row + col,

diags2(rowstail, colstail, diagonaltail).

eight_queens(cols) :-

cols = [_, _, _, _, _, _, _, _],

valid_col(cols),

colindex = [1, 2, 3, 4, 5, 6, 7, 8],

diags1(colindex, cols, diags1),

diags2(colindex, cols, diags2),

all_different(cols),

all_different(diags1),

all_different(diags2).

2 第三天作業

1.通過檢視時間命令將分別將年月日時分寫入a檔案,將當前顯示為星期幾追加寫 入a檔案。或者在螢幕上輸出 the day is 星期幾 解答 root localhost date cut d f 1 5 a.txt root localhost cat a.txt sat feb 27 07 20 ...

第三天python作業

name alex lenb name b name.strip print name b name alex lenb name b name.replace l p 1 print name b name alex lenb name b name.split l print name b na...

第三天,努力!!

第三天了,不錯不錯,基本上按自己的計畫進行著,雖然工作還是沒找到,不過也不再那麼的怕了,慢慢來吧,其實也沒太多時間了 今天早上十點才,真的覺得自己太不像話了,不過我一覺醒來就那個時候了,我的手機每天的鬧鐘是7 30不過好像從來聽不見它響,哈哈 明天希望能早點起,明天北京工體有乙個大型的招聘會,可以去...