September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,76 @@
// version 1.0.6
object RectangleCutter {
private var w: Int = 0
private var h: Int = 0
private var len: Int = 0
private var cnt: Long = 0
private lateinit var grid: ByteArray
private val next = IntArray(4)
private val dir = arrayOf(
intArrayOf(0, -1),
intArrayOf(-1, 0),
intArrayOf(0, 1),
intArrayOf(1, 0)
)
private fun walk(y: Int, x: Int) {
if (y == 0 || y == h || x == 0 || x == w) {
cnt += 2
return
}
val t = y * (w + 1) + x
grid[t]++
grid[len - t]++
(0..3).filter { grid[t + next[it]] == 0.toByte() }
.forEach { walk(y + dir[it][0], x + dir[it][1]) }
grid[t]--
grid[len - t]--
}
fun solve(hh: Int, ww: Int, recur: Boolean): Long {
var t: Int
h = hh
w = ww
if ((h and 1) != 0) {
t = w
w = h
h = t
}
if ((h and 1) != 0) return 0L
if (w == 1) return 1L
if (w == 2) return h.toLong()
if (h == 2) return w.toLong()
val cy = h / 2
val cx = w / 2
len = (h + 1) * (w + 1)
grid = ByteArray(len)
len--
next[0] = -1
next[1] = -w - 1
next[2] = 1
next[3] = w + 1
if (recur) cnt = 0L
for (x in cx + 1 until w) {
t = cy * (w + 1) + x
grid[t] = 1
grid[len - t] = 1
walk(cy - 1, x)
}
cnt++
if (h == w) cnt *= 2
else if ((w and 1) == 0 && recur) solve(w, h, false)
return cnt
}
}
fun main(args: Array<String>) {
for (y in 1..10) {
for (x in 1..y) {
if ((x and 1) == 0 || (y and 1) == 0) {
println("${"%2d".format(y)} x ${"%2d".format(x)}: ${RectangleCutter.solve(y, x, true)}")
}
}
}
}

View file

@ -1,45 +1,46 @@
/*REXX program cuts rectangles into two symmetric pieces, the rectangles are */
/*────────────────────────────── cut along unit dimensions and may be rotated.*/
numeric digits 20 /*be able to handle some big integers. */
parse arg N .; if N=='' then N=10 /*N not specified? Then use default.*/
dir.=0; dir.0.1=-1; dir.1.0=-1; dir.2.1=1; dir.3.0=1 /*4 directions.*/
/*REXX program cuts rectangles into two symmetric pieces, the rectangles are cut along */
/*────────────────────────────────────────────────── unit dimensions and may be rotated.*/
numeric digits 20 /*be able to handle some big integers. */
parse arg N .; if N=='' | N=="," then N=10 /*N not specified? Then use default.*/
dir.=0; dir.0.1=-1; dir.1.0=-1; dir.2.1=1; dir.3.0=1 /*the four directions.*/
do y=2 to N; say /*calculate rectangles up to size NxN.*/
do x=1 for y; if x//2 & y//2 then iterate /*not if both X&Y odd.*/
_=solve(y,x,1); _=right(_,max(10,length(_))) /*align the output. */
say right(y,9) "x" right(x,2) 'rectangle can be cut' _ "way"s(_)'.'
do y=2 to N; say /*calculate rectangles up to size NxN.*/
do x=1 for y; if x//2 & y//2 then iterate /*not if both X&Y odd.*/
z=solve(y,x,1); _=comma(z); _=right(_, max(14, length(_))) /*align the output. */
say right(y,9) "x" right(x,2) 'rectangle can be cut' _ "way"s(z).
end /*x*/
end /*y*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
s: if arg(1)=1 then return arg(3); return word(arg(2) 's',1) /*pluralizer*/
/*────────────────────────────────────────────────────────────────────────────*/
solve: procedure expose # dir. @. h len next. w; @.=0 /*zero rect. coördinates*/
parse arg hh 1 h,ww 1 w,recur /*obtain the values for some arguments.*/
if h//2 then do; t=w; w=h; h=t; if h//2 then return 0
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
comma: procedure; arg _; do k=length(_)-3 to 1 by -3; _=insert(',',_,k); end; return _
/*──────────────────────────────────────────────────────────────────────────────────────*/
s: if arg(1)=1 then return arg(3); return word(arg(2) 's', 1) /*pluralizer.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
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 /*cut the [XY] rectangle in half. */
len = (h+1) * (w+1) - 1 /*extend the area of the rectangle. */
next.0=-1; next.1=-w-1; next.2=1; next.3=w+1 /*direction & distance*/
if h==2 then return w /* [↓] % is REXX's integer division.*/
cy=h % 2; cx=w % 2; wp=w + 1 /*cut the [XY] rectangle in half. */
len=(h+1) * wp - 1 /*extend the area of the rectangle. */
next.0=-1; next.1=-wp; next.2=1; next.3=wp /*direction & distance.*/
if recur then #=0
do x=cx+1 to w-1; t=x+cy*(w+1)
@.t=1; _=len-t; @._=1; call walk cy-1,x
end /*x*/
do x=cx+1 to w-1; t=x + cy*wp; @.t=1; _=len - t; @._=1
call walk cy-1, x
end /*x*/
#=#+1
if h==w then #=#+# /*double the count of rectangle cuts. */
else if w//2==0 & recur then call solve w,h,0
if h==w then #=# + # /*double the count of rectangle cuts. */
else if w//2==0 & recur then call solve w, h, 0
return #
/*────────────────────────────────────────────────────────────────────────────*/
walk: procedure expose # dir. @. h len next. w; parse arg y,x
if y==h | x==0 | x==w | y==0 then do; #=#=2; return; end
t=x + y*(w+1); @.t=@.t+1; _=len-t
/*──────────────────────────────────────────────────────────────────────────────────────*/
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; _ = t+next.j /*try four directions.*/
if @._==0 then call walk y+dir.j.0, x+dir.j.1
do j=0 for 4; _=t + next.j /*try each of four directions.*/
if @._==0 then call walk y + dir.j.0, x + dir.j.1
end /*j*/
@.t=@.t-1
_=len-t; @._=@._-1
return
@.t=@.t - 1
_=len - t; @._=@._ - 1; return

View file

@ -1,55 +1,55 @@
/*REXX program cuts rectangles into two symmetric pieces, the rectangles are */
/*────────────────────────────── cut along unit dimensions and may be rotated.*/
numeric digits 20 /*be able to handle some big integers. */
parse arg N .; if N=='' then N=10 /*N not specified? Then use default.*/
dir.=0; dir.0.1=-1; dir.1.0=-1; dir.2.1=1; dir.3.0=1 /*4 directions.*/
/*REXX program cuts rectangles into two symmetric pieces, the rectangles are cut along */
/*────────────────────────────────────────────────── unit dimensions and may be rotated.*/
numeric digits 20 /*be able to handle some big integers. */
parse arg N .; if N=='' | N=="," then N=10 /*N not specified? Then use default.*/
dir.=0; dir.0.1=-1; dir.1.0=-1; dir.2.1=1; dir.3.0=1 /*the four directions.*/
do y=2 to N; say /*calculate rectangles up to size NxN.*/
do x=1 for y; if x//2 & y//2 then iterate /*not if both X&Y odd.*/
_=solve(y,x,1); _=right(_,max(10,length(_))) /*align the output. */
say right(y,9) "x" right(x,2) 'rectangle can be cut' _ "way"s(_)'.'
do y=2 to N; say /*calculate rectangles up to size NxN.*/
do x=1 for y; if x//2 & y//2 then iterate /*not if both X&Y odd.*/
z=solve(y,x,1); _=comma(z); _=right(_, max(14, length(_))) /*align the output. */
say right(y,9) "x" right(x,2) 'rectangle can be cut' _ "way"s(z).
end /*x*/
end /*y*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
s: if arg(1)=1 then return arg(3); return word(arg(2) 's',1) /*pluralizer*/
/*────────────────────────────────────────────────────────────────────────────*/
solve: procedure expose # dir. @. h len next. w; @.=0 /*zero rect. coördinates*/
parse arg hh 1 h,ww 1 w,recur /*obtain the values for some arguments.*/
if h//2 then do; parse value w h w with t w h; if h//2 then return 0
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
comma: procedure; arg _; do k=length(_)-3 to 1 by -3; _=insert(',',_,k); end; return _
/*──────────────────────────────────────────────────────────────────────────────────────*/
s: if arg(1)=1 then return arg(3); return word(arg(2) 's', 1) /*pluralizer.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
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 /*cut the [XY] rectangle in half. */
len = (h+1) * (w+1) - 1 /*extend the area of the rectangle. */
next.0=-1; next.1=-w-1; next.2=1; next.3=w+1 /*direction & distance*/
if h==2 then return w /* [↓] % is REXX's integer division.*/
cy=h % 2; cx=w % 2; wp=w + 1 /*cut the [XY] rectangle in half. */
len=(h+1) * wp - 1 /*extend the area of the rectangle. */
next.0=-1; next.1=-wp; next.2=1; next.3=wp /*direction & distance.*/
if recur then #=0
do x=cx+1 to w-1; t=x+cy*(w+1)
@.t=1; _=len-t; @._=1; call walk cy-1,x
end /*x*/
do x=cx+1 to w-1; t=x + cy*wp; @.t=1; _=len - t; @._=1
call walk cy-1, x
end /*x*/
#=#+1
if h==w then #=#+# /*double the count of rectangle cuts. */
else if w//2==0 & recur then call solve w,h,0
if h==w then #=# + # /*double the count of rectangle cuts. */
else if w//2==0 & recur then call solve w, h, 0
return #
/*────────────────────────────────────────────────────────────────────────────*/
walk: procedure expose # dir. @. h len next. w; parse arg y,x
if y==h then do; #=#+2; return; end /*◄──┐ REXX short circuit. */
if x==0 then do; #=#+2; return; end /*◄──┤ " " " */
if x==w then do; #=#+2; return; end /*◄──┤ " " " */
if y==0 then do; #=#+2; return; end /*◄──┤ " " " */
t=x + y*(w+1); @.t=@.t+1; _=len-t /* │ordered by most likely ►──┐*/
@._=@._+1 /* └──────────────────────────┘*/
do j=0 for 4; _ = t+next.j /*try 4 directions.*/
if @._==0 then do
yn=y+dir.j.0; xn=x+dir.j.1
if yn==h then do; #=#+2; iterate; end
if xn==0 then do; #=#+2; iterate; end
if xn==w then do; #=#+2; iterate; end
if yn==0 then do; #=#+2; iterate; end
call walk yn, xn
end
end /*j*/
@.t=@.t-1
_=len-t; @._=@._-1
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
walk: procedure expose # dir. @. h len next. w wp; parse arg y,x
if y==h then do; #=#+2; return; end /* ◄──┐ REXX short circuit. */
if x==0 then do; #=#+2; return; end /* ◄──┤ " " " */
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 /* │ordered by most likely ►──┐*/
@._=@._+1 /* └──────────────────────────┘*/
do j=0 for 4; _=t + next.j /*try each of the four directions.*/
if @._==0 then do; yn=y + dir.j.0; xn=x + dir.j.1
if yn==h then do; #=#+2; iterate; end
if xn==0 then do; #=#+2; iterate; end
if xn==w then do; #=#+2; iterate; end
if yn==0 then do; #=#+2; iterate; end
call walk yn, xn
end
end /*j*/
@.t=@.t - 1
_=len - t; @._=@._ - 1; return

View file

@ -0,0 +1,73 @@
fn cwalk(mut vis: &mut Vec<Vec<bool>>, count: &mut isize, w: usize, h: usize, y: usize, x: usize, d: usize) {
if x == 0 || y == 0 || x == w || y == h {
*count += 1;
return;
}
vis[y][x] = true;
vis[h - y][w - x] = true;
if x != 0 && ! vis[y][x - 1] {
cwalk(&mut vis, count, w, h, y, x - 1, d | 1);
}
if d & 1 != 0 && x < w && ! vis[y][x+1] {
cwalk(&mut vis, count, w, h, y, x + 1, d | 1);
}
if y != 0 && ! vis[y - 1][x] {
cwalk(&mut vis, count, w, h, y - 1, x, d | 2);
}
if d & 2 != 0 && y < h && ! vis[y + 1][x] {
cwalk(&mut vis, count, w, h, y + 1, x, d | 2);
}
vis[y][x] = false;
vis[h - y][w - x] = false;
}
fn count_only(x: usize, y: usize) -> isize {
let mut count = 0;
let mut w = x;
let mut h = y;
if (h * w) & 1 != 0 {
return count;
}
if h & 1 != 0 {
std::mem::swap(&mut w, &mut h);
}
let mut vis = vec![vec![false; w + 1]; h + 1];
vis[h / 2][w / 2] = true;
if w & 1 != 0 {
vis[h / 2][w / 2 + 1] = true;
}
let mut res;
if w > 1 {
cwalk(&mut vis, &mut count, w, h, h / 2, w / 2 - 1, 1);
res = 2 * count - 1;
count = 0;
if w != h {
cwalk(&mut vis, &mut count, w, h, h / 2 + 1, w / 2, if w & 1 != 0 { 3 } else { 2 });
}
res += 2 * count - if w & 1 == 0 { 1 } else { 0 };
}
else {
res = 1;
}
if w == h {
res = 2 * res + 2;
}
res
}
fn main() {
for y in 1..10 {
for x in 1..y + 1 {
if x & 1 == 0 || y & 1 == 0 {
println!("{} x {}: {}", y, x, count_only(x, y));
}
}
}
}

View file

@ -0,0 +1,34 @@
fcn cut_it(h,w){
if(h.isOdd){
if(w.isOdd) return(0);
t,h,w=h,w,t; // swap w,h: a,b=c,d --> a=c; b=d; so need a tmp
}
if(w==1) return(1);
nxt :=T(T(w+1, 1,0), T(-w-1, -1,0), T(-1, 0,-1), T(1, 0,1)); #[next, dy,dx]
blen:=(h + 1)*(w + 1) - 1;
grid:=(blen + 1).pump(List(),False); //-->L(False,False...)
walk:='wrap(y,x){ // lambda closure
if(y==0 or y==h or x==0 or x==w) return(1);
count,t:=0,y*(w + 1) + x;
grid[t]=grid[blen - t]=True;
foreach nt,dy,dx in (nxt){
if(not grid[t + nt]) count+=self.fcn(y + dy, x + dx,vm.pasteArgs(2));
}
grid[t]=grid[blen - t]=False;
count
};
t:=h/2*(w + 1) + w/2;
if(w.isOdd){
grid[t]=grid[t + 1]=True;
count:=walk(h/2, w/2 - 1);
count + walk(h/2 - 1, w/2)*2;
}else{
grid[t]=True;
count:=walk(h/2, w/2 - 1);
if(h==w) return(count*2);
count + walk(h/2 - 1, w/2);
}
}

View file

@ -0,0 +1,3 @@
foreach w,h in ([1..9],[1..w]){
if((w*h).isEven) println("%d x %d: %d".fmt(w, h, cut_it(w,h)));
}