Analysis of the problem "Resistors"
(In this problem, it's all the same — to talk about resistors or about capacitors, because the connection formulas are similar, only for resistors for serial connection, the formula is the same as for capacitors for parallel, and vice versa. This does not affect the task. For convenience, I will talk about resistors, not capacitors.)
"Resistors" is a task, let's say, of a "general kind" for brute force. Namely, if in other search tasks you usually form some kind of sequence, sequentially adding something else to the current solution, then it will not work so easily here.
You can try doing this: you have some current schema. You take another unused resistor and attach it to the current circuit either in series or in parallel. Then you take another not yet used resistor, etc. But there is a problem with this solution: you will not be able to get a truly complex scheme. For example, consider the following scheme:
+--D-E-+ +-A--B--+ +--+ -+ +--F-G-+ +--- | | +-G--H--+--I--+---+ | | +-J-K-+
(letters denote resistors)
You will not be able to get it, because here you need to first assemble several separate schemes, and then combine them into one.
You can try to assemble two schemes at once, but still you will not be able to fully cover all the variety of solutions.
Therefore, this task can be solved in a different way. Namely, we reformulate it like this. We have N resistors on the table. We can take two of them, remove them, and instead put a resistor equal to either the two resistors connected in series or connected in parallel. That is, we can replace any two resistors with the result of their serial or parallel connection. After that, we can again take any two resistors (including the one that we just received from the previous two) and replace it with one. Etc. It is clear that this just covers the whole variety of schemes, because no one forces us to build up one specific scheme all the time, we can first assemble several pieces, and then combine them properly.
And it is very easy to implement. The find
procedure will select two resistors, replace them with one, and run recursively.
var r:array[...] of extended; // resistors that are currently lying on the table n:integer; // number of resistors procedure find; var i,j:integer; r1,r2:extended; begin for i:=1 to n do for j:=i+1 to n do begin r1:=r[i]; r2:=r[j]; // the magic begins, understand it! r[j]:=r[n]; dec(n); r[i]:=r1+r2; find; r[i]:=r1*r2/(r1+r2); find; inc(n); r[n]:=r[j]; r[i]:=r1; r[j]:=r2; end; end;
That's all. It is not even necessary to check for the exit from recursion, because when n = 1
becomes, the cycles will not be executed once. It remains only to check all the resulting resistors to see if a good solution is not obtained, it is easiest to do this by cycling through all the available resistors at the beginning of the find procedure.
What's important here. That the solution here is not built by successive additions, but that you honestly explore all possible ways. You can think of it as some kind of game in which there are positions (which resistors are on the table) and possible moves from each position, and you just iterate over such moves.