/* numberlink_b.pi in Picat Taken from The SAT Compiler in B-Prolog http://www.cs.nmsu.edu/ALP/2013/03/the-sat-compiler-in-b-prolog/ by Neng-Fa Zhou */ import sat. main => go. test => INo = 1, inputM(INo,NP,InputM), printf("solving %d%n",INo), once(subMat(NP,InputM.length,InputM[1].length,InputM)). go ?=> inputM(INo,NP,InputM), printf("solving %d%n",INo), once(subMat(NP,InputM.length,InputM[1].length,InputM)), fail. go => true. subMat(NP,NR,NC,InputM) => M = new_array(NP,NR,NC), M :: 0..1, % ensure that no two numbers occupy the same square foreach(R in 1..NR, C in 1..NC) sum([M[P,R,C] : P in 1..NP]) #=1 end, % each end node has one connected neighbor and each interior node has two connected neighbors foreach(P in 1..NP, R in 1..NR, C in 1..NC) Neibs = [M[P,R1,C1] : (R1,C1) in [(R-1,C),(R+1,C),(R,C-1),(R,C+1)],(R1>=1, C1>=1, R1= M[P,R,C] #= 1, sum(Neibs) #= 1 ; M[P,R,C] #=> (sum(Neibs) #= 2) ) end, solve([threads],M), writeout(M,NP,NR,NC). %write the matrix in the better format writeout(M,NP,NR,NC) => foreach(I in 1..NP) write_matrix(M[I],NR,NC) end. write_matrix(M,NR,NC) => foreach(I in 1..NR, J in 1..NC) write(M[I,J]), print(' '), (J==NC->nl;true) end, nl,nl. inputM(INo,NP,M) ?=> INo=0, NP = 2, M = {{1,0}, {2,0}, {2,1}}. % # Numberlink Puzzle % # Author: Otto Janko % # Source: http://www.janko.at/Raetsel/ % # URL: http://www.janko.at/Raetsel/Arukone/109.a.htm % size 10 10 % - - - 6 - - - - - - % - - - - - - - - - - % - - 5 - 1 4 - 3 - - % - - - - 3 - - 8 - - % - 2 - 2 - - - - - - % - 4 - 8 - 7 - - - - % - - - - - - - - - - % - - 5 - 6 1 - 7 - - % - - - - - - - - - - % - - - - - - - - - - inputM(INo,NP,M) ?=> INo=1, NP=8, M={{0,0,0,6,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,5,0,1,4,0,3,0,0}, {0,0,0,0,3,0,0,8,0,0}, {0,2,0,2,0,0,0,0,0,0}, {0,4,0,8,0,7,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,5,0,6,1,0,7,0,0}, {0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0}}. % # Numberlink Puzzle % # Author: The Afis Project % # Source: http://www.afis.to/~start/ % # URL: http://www.janko.at/Raetsel/Arukone/001.a.htm % size 5 5 % 3 - - - 2 % 4 1 2 - - % - - - 3 - % - - - - - % 4 - - - 1 inputM(INo,NP,M) ?=> INo=2, NP=4, M={{3,0,0,0,2}, {4,1,2,0,0}, {0,0,0,3,0}, {0,0,0,0,0}, {4,0,0,0,1}}. % - - - 10 - - - - - - 11 - - - - % - 3 - 6 - - - - - - - - - - 11 % - - - - - - - - - - - - 6 - - % - - - - - - - - 2 - - - - 12 - % - - - - - - - - - - - 9 - - - % 4 - - - 4 - - - - - - - - - - % - - - - - - - - 9 - - - - - - % - - - - - - - - - - - - - - - % - - - - - - - 1 - - 12 - - - - % - - - - - - - - - - - - - 7 - % - - - - 2 - - - - - 10 8 - - - % - - - - - - - - 5 8 - - - - - % - - - - - - - - - - 1 3 5 - - % - - - - - - - - - - - - 7 - - % - - - - - - - - - - - - - - - inputM(INo,NP,M) ?=> INo=3, NP=12, M = {{0,0,0,10,0,0,0,0,0,0,11,0,0,0,0}, {0,3,0,6,0,0,0,0,0,0,0,0,0,0,11}, {0,0,0,0,0,0,0,0,0,0,0,0,6,0,0}, {0,0,0,0,0,0,0,0,2,0,0,0,0,12,0}, {0,0,0,0,0,0,0,0,0,0,0,9,0,0,0}, {4,0,0,0,4,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,9,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,1,0,0,12,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,7,0}, {0,0,0,0,2,0,0,0,0,0,10,8,0,0,0}, {0,0,0,0,0,0,0,0,5,8,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0,0,1,3,5,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,7,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}. % size 42 25 % - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1 % - - - - - - - - - - - - - 12 - - - - - - - - - - - - - - - - - - 25 - - - - - - - - - % - - 5 - - - - - - - - - - 10 - 17 62 - 56 61 60 18 - - - - - - 50 - - - - - - - 49 - 39 - - - % - - - - - - - - - - - - - - - - - - 61 - - - - 34 - - - - - - - 59 - - - - - - - 24 - - % - - - - - - - - - - - - - - - - 62 15 60 - - - - - - 56 - - - 46 - 47 - - - - - - - - 49 - % - - - - - 14 - - - - - - - - 16 19 18 - - - - - - - - 33 - - - - - - 59 - - - - - - - - - % - - - 4 - 13 - - - - - - - - 17 - - - - - 55 53 - 34 - 35 - - - - 48 - - - - - - - - - - - % - - - - - 7 - - - - - - - - - - 13 - - - 54 - - 33 - 57 - - - - - - - - - - - - - - - - % - - - - - 12 - - - - - - - - 3 41 42 - - - 53 - 52 32 - - - - - 57 - - - - - - - 47 - - - - % - - - - - 11 - - - - - - - - - - - - - - - - - - - - 35 - - - - - 45 - - - 58 - - - - - % - - - - - 10 - - - - - - - - - - - - - 55 - - - - 21 - - - 51 - - - - 26 - - 46 - - - - - % - - - - - - 11 5 - - - - - - - - - - - 19 54 - - - - - - - - - - - - - - 58 - - - - - - % - - - - - - - - - - - - - - - - - - - 52 - - - - - - - - - - - - - - - - - - - - - - % - - - - - - - - - - - - - - - - - - - - - 42 - - - - - - - 29 45 - - - - - - - - - - - % - - - - - 9 8 6 - - - 16 - - - - - - - - - - - 32 51 - - - - - - - - - - - 28 - - - - - % - - - - - 8 - - - - - - - - - - - 36 - - 38 - - - 44 50 - 30 - - - - - - - - 27 - - - - - % - - - - - - 7 - - - - - - - - - - - 41 22 - 22 - - - 43 23 31 - - - - - - - - 26 - - - - - % - - - - - 6 - - - - - - - - - - - - 36 - - 21 - - - 27 - - - - - - - - - - 25 - - - - - % - - - - - - - - 9 15 - - 14 - - - - - 37 - 20 44 - - - - - 43 - 31 - - - - - - 24 - - - - - % - - - - - - - - - - - - - - - - - - - - - - - - 38 39 40 28 - 30 - - - - - - 23 - - - - - % - 2 - - - - - - - - - - - - - 4 - - - - 20 - 37 - 48 - - - - 29 - - - - - - - - - - - - % - - 3 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - % - - - 2 - - - - - - - - - - - - - - - - - - - - - 40 - - - - - - - - - - - - - - - - % - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - % 1 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - inputM(INo,NP,M) => INo=4, NP=63, M = {{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}, {0,0,0,0,0,0,0,0,0,0,0,0,0,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,25,0,0,0,0,0,0,0,0,0}, {0,0,5,0,0,0,0,0,0,0,0,0,0,10,0,17,62,0,56,61,60,18,0,0,0,0,0,0,50,0,0,0,0,0,0,0,49,0,39,0,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,61,0,0,0,0,34,0,0,0,0,0,0,0,59,0,0,0,0,0,0,0,24,0,0}, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,62,15,60,0,0,0,0,0,0,56,0,0,0,46,0,47,0,0,0,0,0,0,0,0,49,0}, {0,0,0,0,0,14,0,0,0,0,0,0,0,0,16,19,18,0,0,0,0,0,0,0,0,33,0,0,0,0,0,0,59,0,0,0,0,0,0,0,0,0}, {0,0,0,4,0,13,0,0,0,0,0,0,0,0,17,0,0,0,0,0,55,53,0,34,0,35,0,0,0,0,48,0,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