home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / eiffel / smalleif.97 / se.t / SmallEiffel / lib_show / pyramid2.e < prev    next >
Encoding:
Text File  |  1996-05-02  |  3.4 KB  |  169 lines

  1. -- Part of SmallEiffel -- Read DISCLAIMER file -- Copyright (C) 
  2. -- Dominique COLNET and Suzanne COLLIN -- colnet@loria.fr
  3. --
  4. class PYRAMID2
  5. --
  6. --| Auteur : Christophe Alexandre
  7. --|  date  : Tue Feb 27 14:39:12 1996
  8. --|
  9. --| Run faster than pyramid.e   
  10.    
  11. inherit ANY redefine print_on end;
  12.    
  13. creation {ANY}
  14.    make
  15.    
  16. feature {NONE}
  17.    
  18.    pyramid: ARRAY2[INTEGER]; 
  19.    
  20.    used: ARRAY[BOOLEAN]; -- Already used numbers in `pyramid'.
  21.    
  22.    make is
  23.       do
  24.      from
  25.         ask 
  26.      until
  27.         io.last_integer > 1
  28.      loop
  29.         ask;
  30.      end
  31.      io.put_string("Please wait, I working...%N");
  32.      fill(io.last_integer);
  33.       end; -- make
  34.    
  35.    ask is
  36.       do
  37.      io.put_string("Enter the size of the pyramid %
  38.             % (a small number greater than 1) : ");
  39.      io.read_integer;
  40.      io.put_new_line;
  41.       end; -- ask
  42.    
  43.    fill(size : INTEGER) is
  44.      -- Fill in a `pyramid' of `size' elements. 
  45.       require
  46.      size > 1;
  47.       do
  48.      !!used.make(1,(size+1)*size // 2);
  49.      !!pyramid.make(1,size,1,size);
  50.      if solution(1) then
  51.         print_on(std_output)
  52.      else
  53.         io.put_string("Sorry I can't find a solution.%N");
  54.      end;
  55.       end; -- fill
  56.    
  57.    put(value,line,column: INTEGER) is
  58.      -- Updtate `pyramid' and `used'. 
  59.       do
  60.      used.put(true,value);
  61.      pyramid.put(value,line,column);
  62.       end; -- put
  63.    
  64.    remove(line,column :INTEGER) is
  65.      -- Updtate `pyramid' and `used'. 
  66.       do 
  67.      if pyramid.item(line,column) /= 0 then
  68.         used.put(false,pyramid.item(line,column));
  69.         pyramid.put(0,line,column);
  70.      end;
  71.       end; -- remove
  72.    
  73.    solution(column:INTEGER): BOOLEAN is
  74.      -- Search a solution using a back-tracking algorithm.
  75.       local 
  76.      nb,i: INTEGER;
  77.       do
  78.      if column > pyramid.upper1 then
  79.         Result := true;
  80.      else
  81.         from
  82.            nb := used.upper
  83.         until
  84.            Result or nb = 0
  85.         loop
  86.            if not used.item(nb) then
  87.           Result := fill_column(column,nb)
  88.            end;
  89.            if not Result then
  90.           from
  91.              i := pyramid.lower1
  92.           until
  93.              pyramid.item(i,column) = 0 or else i > pyramid.upper1 
  94.           loop
  95.              remove(i,column);
  96.              i := i + 1;
  97.           end;
  98.            end;
  99.            nb := nb - 1;
  100.         end;
  101.      end;
  102.       end; -- solution
  103.    
  104.    fill_column(col,val: INTEGER): BOOLEAN is
  105.       local
  106.      v, i: INTEGER;
  107.      break: BOOLEAN; 
  108.       do
  109.      from 
  110.         i := 2;
  111.         put(val,1,col);
  112.      until
  113.         i > col or Result 
  114.      loop
  115.         v := (pyramid.item(i-1,col-1)-pyramid.item(i-1,col)).abs
  116.         if used.item(v) then
  117.            Result := true;
  118.         else
  119.            put(v,i,col);
  120.            i := i+1;
  121.         end;
  122.      end;
  123.      if Result then
  124.         from
  125.         until
  126.            i < used.lower
  127.         loop
  128.            remove(i,col);
  129.            i := i-1;
  130.         end;
  131.         Result := false;
  132.      else
  133.         Result := solution(col+1);
  134.      end;
  135.       end; -- fill_column
  136.    
  137.    print_on(file: STD_FILE_WRITE) is
  138.      -- display the pyramid to the standart output.
  139.       local
  140.      line,column : INTEGER;
  141.      blanks : STRING;
  142.       do
  143.      from
  144.         file.put_string("%NSolution found : %N");
  145.         line := pyramid.lower1;
  146.         !!blanks.make(0);
  147.      until
  148.         line > pyramid.upper1
  149.      loop
  150.         file.put_string(blanks);
  151.         from
  152.            column := pyramid.lower2
  153.         until
  154.            column > pyramid.upper2
  155.         loop
  156.            if pyramid.item(line,column) /= 0 then
  157.           file.put_integer(pyramid.item(line,column));
  158.           file.put_string(" ");
  159.            end;
  160.            column := column+1;
  161.         end;
  162.         file.put_new_line;
  163.         line := line+1;
  164.         blanks.add_last(' ');
  165.      end;   
  166.       end; -- print_on
  167.    
  168. end -- PYRAMID2
  169.