with colas3;
with text_io;
use text_io;
procedure ochoreinas2 is
	package micola is new colas3(integer);
	use micola;
	package int_io is new integer_io(Integer);
	use int_io;
	type solucion is array(1..8) of integer;
	type continuacion is array(1..8) of cola;

procedure escribe(s:in solucion) is
begin
	put('(');
	put(s(1),2);
	for i in 2..8 loop
		put(',');
		put(s(i),2);
	end loop;
	put_line(")");
end escribe;

function valida(s:solucion; k,i:integer) return
boolean is
-- True si puedo hacer s(k)=i
respuesta: boolean;
begin
	respuesta:=true;
	for j in 1..k-1 loop
		respuesta:=respuesta and s(j)/=i and
		(j-s(j))/=(k-i) and (j+s(j))/=(k+i);
	end loop;	
	return respuesta;
end valida;

procedure calcularCont(s: in solucion; c:in out
continuacion; k: in
integer) is
-- calcula las posibles continuaciones de s en c(k)
begin
	creaVacia(c(k));
	for i in 1..8 loop
		if valida(s,k,i) then encolar(c(k),i); end if;
	end loop;
end calcularCont;		

s: solucion;
k: integer;
c: continuacion;
-- la solución parcial calculada es s(1..k)
-- las continuaciones para k+1 están en c(k+1)
begin
	k:=0;
	calcularCont(s,c,1);
	while k>0 or not esVacia(c(1)) loop
		if k<8 and then not esVacia(c(k+1)) then
		-- extender la solucion
			k:=k+1;
			s(k):=primero(c(k));
			desencolar(c(k));
			if k<8 then 
				calcularCont(s,c,k+1);
			else
				escribe(s);
			end if;
		else
		-- backtracking
			k:=k-1;
		end if;		
	end loop;
end ochoreinas2;	


syntax highlighted by Code2HTML, v. 0.9.1