/* Table of numbers problem in Picat. From Fun With Num3ers "Table of numbers with 5 rows and 5 columns" http://benvitalenum3ers.wordpress.com/2012/03/15/table-of-numbers-with-5-rows-and-5-columns/ """ Below is a table of numbers with 5 rows and 5 columns. From the table, choose 5 numbers in such a way that no two numbers come from the same row and no two numbers come from the same column. Find the sum of these 5 numbers. Explain the result. 7 8 5 9 10 6 7 4 8 9 9 10 7 11 12 8 9 6 10 11 4 5 2 6 7 """ There are 120 (=5!) different solutions to this problem. This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => T = [[7, 8, 5, 9, 10], [6, 7, 4, 8, 9], [9, 10, 7, 11, 12], [8, 9, 6, 10, 11], [4, 5, 2, 6, 7]], N = T.len, X = new_array(N,N), X :: 0..1, foreach(I in 1..N) sum([X[I,J] : J in 1..N]) #= 1, sum([X[J,I] : J in 1..N]) #= 1 end, TheSum #= sum([X[I,J]*T[I,J] : I in 1..N, J in 1..N]), solve(X), println(sum=TheSum), foreach(I in 1..N) foreach(J in 1..N) printf("%2d ", X[I,J]*T[I,J]), end, nl end, nl, fail, nl.