Jump to content

Water collected between towers: Difference between revisions

no edit summary
(Added solution for Pascal)
No edit summary
Line 1,886:
Block 6 does not hold any water units.
Block 7 does not hold any water units.</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
The program builds a matrix of the towers and scans each line looking for pairs of towers that trap water.
 
<syntaxhighlight lang="Delphi">
 
var IA: array [0..9] of integer = (5, 3, 7, 2, 6, 4, 5, 9, 1, 2);
 
type TMatrix = array of array of boolean;
 
function ArrayToMatrix(IA: array of integer): TMatrix;
var Max,I,X,Y: integer;
begin
Max:=0;
for I:=0 to High(IA) do if IA[I]>=Max then Max:=IA[I];
SetLength(Result,Length(IA),Max);
for Y:=0 to High(Result[0]) do
for X:=0 to High(Result) do Result[X,Y]:=IA[X]>(Max-Y);
end;
 
 
procedure DisplayMatrix(Memo: TMemo; Matrix: TMatrix);
var X,Y: integer;
var S: string;
begin
for Y:=0 to High(Matrix[0]) do
begin
S:='[';
for X:=0 to High(Matrix) do
begin
if Matrix[X,Y] then S:=S+'#'
else S:=S+' ';
end;
S:=S+']';
Memo.Lines.Add(S);
end;
end;
 
 
procedure WaterLevel(Memo: TMemo);
var I,X,Y,Sum,Cnt: integer;
var Max: integer;
var Matrix: TMatrix;
var S: string;
var Inside: boolean;
begin
Matrix:=ArrayToMatrix(IA);
DisplayMatrix(Memo,Matrix);
Sum:=0;
for Y:=0 to High(Matrix[0]) do
begin
Inside:=False;
Cnt:=0;
for X:=0 to High(Matrix) do
begin
if Matrix[X,Y] then
begin
Inside:=True;
Sum:=Sum+Cnt;
Cnt:=0;
end
else if Inside then Inc(Cnt);
end;
end;
Memo.Lines.Add(IntToStr(Sum));
end;
 
procedure ShowWaterLevels(Memo: TMemo);
begin
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
[ ]
[ # ]
[ # ]
[ # # ]
[ # # ]
[ ### ]
[ ####]
Storage: 2
 
[ ]
[ # ]
[ # ]
[ # # ]
[ # # # ]
[# # # ## ]
[# # #### ]
[### #### ]
[######## #]
Storage: 14
 
[ ]
[ # ]
[ # # ]
[ # # # ]
[ # # # # ## ]
[ # # # # # ### ]
[ ### # # ##### ]
[###### ######## ]
Storage: 35
 
[ ]
[####]
[####]
[####]
[####]
Storage: 0
 
[ ]
[ #]
[ ##]
[ ###]
[####]
[####]
[####]
[####]
Storage: 0
 
[ ]
[# ]
[### ]
[####]
[####]
[####]
[####]
[####]
Storage: 0
 
[ ]
[ # ]
[ # ]
[ # ]
[ ### ]
[#####]
[#####]
[#####]
[#####]
[#####]
Storage: 0
Elapsed Time: 171.444 ms.
</pre>
 
 
=={{header|Erlang}}==
465

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.