Re: a problem in my program in pascal
- From: "Maarten Wiltink" <maarten@xxxxxxxxxxxxxxxxxx>
- Date: Wed, 11 Oct 2006 17:16:46 +0200
<gilevgi@xxxxxxxxx> wrote in message
news:1160569433.506935.220260@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I want to write the pattern " for i:=1 to a[i] do" n times (n is
variable), that is,
for b[1]:=1 to a[1] do
for b[2]:=1 to a[2] do
for b[3]:=1 to a[3] do
for b[4]:=1 to a[4] do
.........
..........
.........
for b[n]:=1 to a[n] do
something here.
in this writing n is a variable. therefore I can not write what i want.
can you say how can i write the above structure using "repeat-until" or
"while" patterns?
Last time I tried this, for solving a nine-tile puzzle[0], I gave up and
wrote out the loops. But it can be done; I've attended lectures where
it was. They were about backtracking and clearly I'd forgotten too much
about them.
First, let me outline a solution that does _not_ involve backtracking.
Assuming you have constant dimensions (a[2] does not vary for different
values of b[1]), multiply them out and have a single loop from 1 to
a[1]*a[2]*a[3]*...*a[n]. Using div and mod appropriately, all the b[]s
can be computed from the loop index.
That's neither very pretty nor very efficient, however. Div and mod may
be pretty when used correctly (and here, it feels like they're the right
solution to the wrong problem), but they're never particularly efficient.
My best guess for backtracking seems a little awkward, too, but as I've
explained that's probably because I'm no longer very good at it. Note also
that normally, backtracking is associated with _truncating_ a solution
tree whenever you find you can no longer produce a viable solution on
the current path. This element is not in your question; you might have
conditional Break statements in every loop to represent it. (For example,
there might be a requirement that all the b[]s added together don't
exceed a certain limit. A partial sum b[1]+b[2]+b[3] might disqualify
an entire branch without the need to loop past b[3].)
type TNumbers = array of Integer;
procedure Loop(const Dimension: Integer; const Indices, Counts: TNumbers);
begin
Assert(Length(Indices)=Length(Counts));
if (Dimension<Length(Indices))
then begin
for Indices[Dimension]:=1 to Counts[Dimension]
do Loop(Succ(Dimension), Indices, Counts);
end;
end
else Work(Indices);
end;
Call as follows:
SetLength(Counts, N);
{ Fill Counts }
SetLength(Indices, N);
Loop(0, Indices, Counts);
I tested it and it works. That is not to say that the above code should
compile - it should not. I've left an easily corrected mistake in it on
purpose, also because it's clearer this way. _And_ you have to implement
Work(). Note that it might be a parameter to Loop().
Groetjes,
Maarten Wiltink
[0] Pieces can end up in any of the positions, in any of four
orientations. It was a surprisingly difficult puzzle.
.
- References:
- a problem in my program in pascal
- From: gilevgi
- a problem in my program in pascal
- Prev by Date: Re: a problem in my program in pascal
- Next by Date: Re: a problem in my program in pascal
- Previous by thread: Re: a problem in my program in pascal
- Next by thread: Re: a problem in my program in pascal
- Index(es):
Relevant Pages
|