2017-10-06 55 views
0

給定一個10 * 10矩陣,填充0〜9。我們只需要在每行和每列垂直列中選擇3個整數,以確保我們選擇的整數在矩陣中最大。如何使用CLIPS(c語言集成生產系統)?附:每行和垂直列只能有0〜9個一次。在矩陣中使用CLIPS進行選擇

+0

目前還不清楚你在問什麼。如果每一行/列在每一列/行中只有一個0-9,你會選擇9,8和7. –

+0

是的,似乎我應該選擇9,8和7.那麼我怎樣才能使用CLIPS選擇它們? –

回答

0

這裏是你如何找到的元素爲行

CLIPS> 
(deftemplate element 
    (slot row) 
    (slot column) 
    (slot value)) 
CLIPS>  
(deffacts start 
    (element (row 1) (column 1) (value 3)) 
    (element (row 1) (column 2) (value 5)) 
    (element (row 1) (column 3) (value 9)) 
    (element (row 1) (column 4) (value 8)) 
    (element (row 1) (column 5) (value 1)) 
    (element (row 1) (column 6) (value 7)) 
    (element (row 1) (column 7) (value 2)) 
    (element (row 1) (column 8) (value 4)) 
    (element (row 1) (column 9) (value 0)) 
    (element (row 1) (column 10) (value 6)) 

    (element (row 2) (column 1) (value 1)) 
    (element (row 2) (column 2) (value 3)) 
    (element (row 2) (column 3) (value 5)) 
    (element (row 2) (column 4) (value 7)) 
    (element (row 2) (column 5) (value 9)) 
    (element (row 2) (column 6) (value 0)) 
    (element (row 2) (column 7) (value 8)) 
    (element (row 2) (column 8) (value 6)) 
    (element (row 2) (column 9) (value 4)) 
    (element (row 2) (column 10) (value 2))) 
CLIPS>  
(defrule largest-in-row 
    (element (row ?r) (column ?c1) (value ?v1)) 
    (not (element (row ?r) (column ~?c1) (value ?v&:(> ?v ?v1)))) 
    (element (row ?r) (column ?c2&~?c1) (value ?v2)) 
    (not (element (row ?r) (column ~?c1&~?c2) (value ?v&:(> ?v ?v2)))) 
    (element (row ?r) (column ?c3&~?c2&~?c1) (value ?v3)) 
    (not (element (row ?r) (column ~?c1&~?c2&~?c3) (value ?v&:(> ?v ?v3)))) 
    => 
    (printout t "r" ?r " : c" ?c1 "=" ?v1 " c" ?c2 "=" ?v2 " c" ?c3 "=" ?v3 crlf)) 
CLIPS> (reset) 
CLIPS> (run) 
r2 : c5=9 c7=8 c4=7 
r1 : c3=9 c4=8 c6=7 
CLIPS>