1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
///////////////////////////////////////////////////////////////////////////////
// Lisaac Example //
// //
// LSIIT - ULP - CNRS - INRIA - FRANCE //
// //
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
// //
// http://isaacproject.u-strasbg.fr/ //
///////////////////////////////////////////////////////////////////////////////
Section Header
+ name := PYRAMID2;
- author := "Sonntag Benoit (bsonntag@loria.fr)";
- comment := "Run faster than pyramid.e";
Section Inherit
- parent_object:OBJECT := OBJECT;
Section Private
+ pyramid:ARRAY2[INTEGER];
+ used:ARRAY[BOOLEAN]; // Already used numbers in `pyramid'.
- fill size:INTEGER <-
// Fill in a `pyramid' of `size' elements.
(
? {size > 1};
used := ARRAY[BOOLEAN].create 1 to ((size+1) * size / 2);
pyramid := ARRAY2[INTEGER].create (1,1) to (size,size);
(solution 1).if {
print;
} else {
"Sorry I can't find a solution.\n".print;
};
);
- put value:INTEGER to (line,column:INTEGER) <-
// Updtate `pyramid' and `used'.
(
used.put TRUE to value;
pyramid.put value to (line,column);
);
- remove (line,column:INTEGER) <-
// Updtate `pyramid' and `used'.
(
(pyramid.item (line,column) != 0).if {
used.put FALSE to (pyramid.item (line,column));
pyramid.put 0 to (line,column);
};
);
- solution column:INTEGER :BOOLEAN <-
// Search a solution using a back-tracking algorithm.
( + nb,i:INTEGER;
+ result:BOOLEAN;
(column > pyramid.upper1).if {
result := TRUE;
} else {
nb := used.upper;
{ result | (nb = 0)}.until_do {
(! used.item nb).if {
result := fill_column column with nb;
};
(! result).if {
i := pyramid.lower1;
{(pyramid.item (i,column) = 0) || { i > pyramid.upper1 }}.until_do {
remove (i,column);
i := i + 1;
};
};
nb := nb - 1;
};
};
result
);
- fill_column col:INTEGER with val:INTEGER :BOOLEAN <-
( + v,i:INTEGER;
+ result:BOOLEAN;
i := 2;
put val to (1,col);
{(i > col) | result}.until_do {
v := (pyramid.item ((i-1),(col-1)) - pyramid.item ((i-1),col)).abs;
(used.item v).if {
result := TRUE;
} else {
put v to (i,col);
i := i + 1;
};
};
result.if {
{i < used.lower}.until_do {
remove (i,col);
i := i-1;
};
result := FALSE;
} else {
result := solution (col+1);
};
result
);
- print <-
// Display the pyramid to the standart output.
( + blanks:STRING;
"\nSolution found : \n".print;
blanks := STRING.create 10;
pyramid.lower1.to (pyramid.upper1) do { line:INTEGER;
blanks.print;
pyramid.lower2.to (pyramid.upper2) do { column:INTEGER;
(pyramid.item (line,column) != 0).if {
pyramid.item (line,column).print;
' '.print;
};
};
'\n'.print;
blanks.add_last ' ';
};
);
Section Public
- main <-
( + size: INTEGER;
// Read argument.
(COMMAND_LINE.upper = 0).if {
"Size of the pyramid (a small number greater than 1) : ".print;
IO.read_integer;
size := IO.last_integer;
} else {
size := COMMAND_LINE.item 1.to_integer;
};
"I am working...\n".print;
fill size;
);
|