Anonymous user
Cut a rectangle: Difference between revisions
m
→{{header|REXX}}: added.changed whitespace and comments, optimized the SOLVE functions, addhighlighting to the REXX section header.
m (→idiomatic: added whitespace.) |
m (→{{header|REXX}}: added.changed whitespace and comments, optimized the SOLVE functions, addhighlighting to the REXX section header.) |
||
Line 2,720:
s: if arg(1)=1 then return arg(3); return word( arg(2) 's', 1) /*pluralizer.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
solve: procedure expose # dir. @. h len next. w; @.=
parse arg h,w,recur /*get values for some args. */
if h//2 then do; t= w; w= h; h= t; if h//2 then return 0
Line 2,727:
if w==2 then return h
if h==2 then return w /* [↓] % is REXX's integer ÷*/
cy= h % 2; cx= w % 2;
len= (h+1) * wp - 1 /*extend area of rectangle. */
next.0= '-1'; next.1= -wp;
if recur then #= 0
do x=cx+1 to w-1; t= x + cy*wp; @.t= 1
Line 2,735:
end /*x*/
#= # + 1
if h==w then #= # + # /*double rectangle cut count.
else if w//2==0 & recur then call solve w, h, 0
return #
/*──────────────────────────────────────────────────────────────────────────────────────*/
walk: procedure expose # dir. @. h len next. w wp; parse arg y,x
if y==h | x==0 | x==w | y==0 then do; #= # + 2; return; end
t= x + y*wp; @.t= @.t + 1; _= len - t
@._= @._ + 1
do j=0 for 4;
if @._==0 then call walk y + dir.j.0, x + dir.j.1
end /*j*/
@.t= @.t - 1
_= len - t;
{{out|output|text= when using the default input:}}
<pre>
Line 2,802:
===optimized===
This version replaced the (first) multiple clause '''if''' instructions in the '''walk''' subroutine with a
<br>''short circuit'' version. Other optimizations were also made. This made the program about '''20%''' faster.
<br><br>A test run was executed to determine the order of the '''if''' statements (by counting which
<br>comparison would yield the most benefit by placing it first).
Line 2,824:
solve: procedure expose # dir. @. h len next. w; @.= 0 /*zero rectangle coördinates.*/
parse arg h,w,recur /*get values for some args. */
if h//2 then do; t= w; w= h; h= t; if h//2 then return 0
end
if w==1 then return 1
if w==2 then return h
if h==2 then return w /* [↓] % is REXX's integer division.*/
cy= h % 2; cx= w % 2; wp= w + 1
len= (h+1) * wp - 1 /*extend the area of the rectangle. */
next.0= '-1'; next.1= -wp; next.2= 1; next.3= wp
if recur then #= 0
do x=cx+1 to w-1; t= x + cy*wp; @.t= 1
Line 2,846:
if x==w then do; #= #+2; return; end /* ◄──┤ " " " */
if y==0 then do; #= #+2; return; end /* ◄──┤ " " " */
t= x + y*wp; @.t= @.t + 1; _= len - t
@._= @._ + 1 /* └──────────────────────────┘*/
do j=0 for 4; _= t + next.j /*try each of the four directions.*/
|