Take a standard 8x8 chessboard, number the black squares 1 and the white squares 0. How many palindromes does this board contain in 'word-search' fashion?
Now generalize and find a formula for any n*n board.
Notes: A palindrome and its reversal should not be counted twice. A palindrome must be at least two digits long and leading zeroes are not allowed. Numbers can be read in any single vertical, horizontal or diagonal direction.
(In reply to
partial solution by Justin)
The formula given for number of diagonal palindromes:
1/12(4(N^3)-6(N^2)+2N+6((-1)^N-1)(N-1)
works for even N, but not for odd, as shown by the following table:
N # of palind diag palind above formula
2 1 1 1
3 6 4 3
4 22 14 14
5 46 28 26
6 91 55 55
7 148 88 85
8 236 140 140
9 340 200 196
10 485 285 285
This assumes that for odd N, the corners are white.
DECLARE FUNCTION bd! (row!, col!)
DIM SHARED ct
CLS
n = 8
FOR n = 2 TO 10
ct = 0
dCt = 0
FOR row = 1 TO n
FOR col = 1 TO n
IF bd(row, col) THEN
FOR c = col + 1 TO n
IF bd(row, c) THEN
ct = ct + 1
END IF
NEXT c
FOR r = row + 1 TO n
FOR c = 1 TO n
IF c = col OR ABS(row - r) = ABS(col - c) THEN
IF bd(r, c) THEN
ct = ct + 1
IF c <> col THEN
dCt = dCt + 1
END IF
END IF
END IF
NEXT c
NEXT r
END IF
NEXT col
NEXT row
PRINT n, ct, dCt, 1 / 12 * (4 * (n ^ 3) - 6 * (n ^ 2) + 2 * n + 6 * ((-1) ^ n - 1) * (n - 1))
NEXT
FUNCTION bd (row, col)
bd = (row + col) MOD 2
END FUNCTION
(it assumes the top-left is white, but that does not matter when N is even)
If we assume the corners for odd N are black, the figures are even more discrepant for odd N:
2 1 1 1
3 10 6 3
4 22 14 14
5 54 32 26
6 91 55 55
7 160 94 85
8 236 140 140
9 356 208 196
10 485 285 285
(the above by changing to:
FUNCTION bd (row, col)
bd = (row + col + 1) MOD 2
END FUNCTION
)
The algorithm merely checks the first and last digits for a 1.
|
Posted by Charlie
on 2006-06-11 09:52:16 |