algorithm - Simple way to calculate number of chess material combinations -
in chess, 1 player can have different material combinations, example:
"1 queen, 2 rooks, 2 knights, 2 bishops, 8 pawns + king" 1 combination
if player loses 1 bishop:
"1 queen, 2 rooks, 2 knights, 1 bishop, 8 pawns + king" combination
..afterwards, if pawn promoted knight, then:
"1 queen, 2 rooks, 3 knights, 1 bishop, 7 pawns + king" combination
ok, following combination not valid:
"5 queens, 5 rooks, 5 knights, 5 bishops, 2 pawns + king"
since lack of pawns promote. (5 queens = 4 pawns needed) (5 rooks = 3 pawns needed) , etc. 4 + 3 + 3 + 3 = 13 pawns needed. since 2 pawns on board, @ 6 pawns promoted. not valid.
how many valid material combinations there? computed 8694 combinations using following c code. question is:
do find simpler/efficient algorithm calculate it? (less cycles, less calculations, clearer code, etc.) ... or math formulae??
total = 0; (queens=0;queens<=9;queens++) (rooks=0;rooks<=10;rooks++) (bishops=0;bishops<=10;bishops++) (knights=0;knights<=10;knights++) (pawns=0;pawns<=8;pawns++) { pawnsrequested = 0; if (queens>1) pawnsrequested += queens - 1; if (rooks>2) pawnsrequested += rooks - 2; if (bishops>2) pawnsrequested += bishops - 2; if (knights>2) pawnsrequested += knights - 2; if (8-pawns < pawnsrequested) continue; total++; } printf("%i\n",total);
if piece types independent, multiply: 10 possibilities queens times 11 possibilities rooks times etc. need track pawn usage, however. there's mathematical trick called generating functions can encode possibilities for, e.g., rooks as
3 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8,
where power of x
denotes number of pawns used, , coefficient denotes number of possibilities. here, there 3 possibilities require no promoted pawns (0, 1, 2), 1 requires 1 promoted pawn (3), 1 requires 2 promoted pawns (4), etc. can multiply each of factors (respectively, queens, rooks, bishops, knights, pawns).
(2 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8) * (3 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8) * (3 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8) * (3 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8) * (1 + x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8)
here wolfram alpha.
the coefficients of 1
through x^8
, number of possibilities 0
8
pawns required, 54, 135, 261, 443, 693, 1024, 1450, 1986, 2648, summing 8694.
Comments
Post a Comment