% This is a demo on how easy it is to produce a list of subgrids from a % Sudoku grid. % We rely on the library predicate transpose/2, and the predicate chop/3 from % Homework P5. % To make it easier to understand the transformation we define a helper % predicate that generates a matrix filled with two digit numbers RC, where % R is the row number and C is the column number. | ?- N = 4, sample_grid(N, G). N = 4, G = [ [11,12,13,14], [21,22,23,24], [31,32,33,34], [41,42,43,44] ] ? yes % Here is what chop does to a 4 element list: | ?- chop(2, [a,b,c,d], L). L = [ [a, b ], [c, d ] ] ? yes % Step 1: % step1(N, Grid, ChoppedGrid): chop Grid to chunks of size sqrt(N), % resulting in ChoppedGrid | ?- N = 4, sample_grid(N, G), step1(N, G, ChoppedG). N = 4, G = [ [11,12,13,14], [21,22,23,24], [31,32,33,34], [41,42,43,44] ], ChoppedG = [ [ [11,12,13,14], [21,22,23,24] ], [ [31,32,33,34], [41,42,43,44] ] ] ? yes % Step 2: % step2(ChoppedG, TChoppedG): transpose each element of list ChoppedG, % resulting in TChoppedG. | ?- N = 4, sample_grid(N, G), step1(N, G, ChoppedG), step2(ChoppedG, TChG). N = 4, G = [ [11,12,13,14], [21,22,23,24], [31,32,33,34], [41,42,43,44] ], ChoppedG = [ [ [11,12,13,14], [21,22,23,24] ], [ [31,32,33,34], [41,42,43,44] ] ], TChG = [ [ [11,21], [12,22], [13,23], [14,24] ], [ [31,41], [32,42], [33,43], [34,44] ] ] ? yes % Step 3: % step3(N, TChG, SGs): Convert TChG, the output of Step 2, to the list of % subgrids SGs. % A possible approach: flatten TChG, then chop it to chunks of size N. % Note that the flatten/2 predicate of SWI Prolog is not available in % SICStus. Write your own one, or use append/2. | ?- N = 4, sample_grid(N, G), step1(N, G, ChG), step2(ChG, TChG), step3(N, TChG, SGs). N = 4, G = [ [11,12,13,14], [21,22,23,24], [31,32,33,34], [41,42,43,44] ], ChG = [ [ [11,12,13,14], [21,22,23,24] ], [ [31,32,33,34], [41,42,43,44] ] ], TChG = [ [ [11,21], [12,22], [13,23], [14,24] ], [ [31,41], [32,42], [33,43], [34,44] ] ], SGs = [ [11,21,12,22], [13,23,14,24], [31,41,32,42], [33,43,34,44] ] ? yes % Example 2: obtaining the subgrids of a 9 x 9 Sudoku grid. | ?- N = 9, sample_grid(N, G), step1(N, G, ChG), step2(ChG, TChG), step3(N, TChG, SGs). N = 9, G = [ [11,12,13,14,15,16,17,18,19], [21,22,23,24,25,26,27,28,29], [31,32,33,34,35,36,37,38,39], [41,42,43,44,45,46,47,48,49], [51,52,53,54,55,56,57,58,59], [61,62,63,64,65,66,67,68,69], [71,72,73,74,75,76,77,78,79], [81,82,83,84,85,86,87,88,89], [91,92,93,94,95,96,97,98,99] ], ChG = [ [ [11,12,13,14,15,16,17,18,19], [21,22,23,24,25,26,27,28,29], [31,32,33,34,35,36,37,38,39] ], [ [41,42,43,44,45,46,47,48,49], [51,52,53,54,55,56,57,58,59], [61,62,63,64,65,66,67,68,69] ], [ [71,72,73,74,75,76,77,78,79], [81,82,83,84,85,86,87,88,89], [91,92,93,94,95,96,97,98,99] ] ], TChG = [ [ [11,21,31], [12,22,32], [13,23,33], [14,24,34], [15,25,35], [16,26,36], [17,27,37], [18,28,38], [19,29,39] ], [ [41,51,61], [42,52,62], [43,53,63], [44,54,64], [45,55,65], [46,56,66], [47,57,67], [48,58,68], [49,59,69] ], [ [71,81,91], [72,82,92], [73,83,93], [74,84,94], [75,85,95], [76,86,96], [77,87,97], [78,88,98], [79,89,99] ] ], SGs = [ [11,21,31,12,22,32,13,23,33], [14,24,34,15,25,35,16,26,36], [17,27,37,18,28,38,19,29,39], [41,51,61,42,52,62,43,53,63], [44,54,64,45,55,65,46,56,66], [47,57,67,48,58,68,49,59,69], [71,81,91,72,82,92,73,83,93], [74,84,94,75,85,95,76,86,96], [77,87,97,78,88,98,79,89,99] ] ? yes | ?-