Programmer Puzzle: Encoding a chess board state throughout a game

前端 未结 30 1537
闹比i
闹比i 2021-01-29 17:16

Not strictly a question, more of a puzzle...

Over the years, I\'ve been involved in a few technical interviews of new employees. Other than asking the standard \"do you

30条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-29 17:52

    Algorithm should deterministically enumerate all possible destinations at each move. Number of destinations:

    • 2 bishops, 13 destinations each = 26
    • 2 rooks, 14 destinations each = 28
    • 2 knights, 8 destinations each = 16
    • queen, 27 destinations
    • king , 8 destinations

    8 paws could all become queens in worst (enumeration-wise) case, thus making largest number of possible destinations 9*27+26+28+16+8=321. Thus, all destinations for any move can be enumerated by a 9 bit number.

    Maximum number of moves of both parties is 100 (if i'm not wrong, not a chess player). Thus any game could be recorded in 900 bits. Plus initial position each piece can be recorded using 6 bit numbers, which totals to 32*6 =192 bits. Plus one bit for "who moves first" record. Thus, any game can be recorded using 900+192+1=1093 bits.

提交回复
热议问题