Browse Source

update: common utils

legacy
Dnomd343 2 years ago
parent
commit
9f978fcf6b
  1. 40
      legacy/main-old.cc
  2. 13
      src/klotski/utils/common.cc
  3. 4
      src/klotski/utils/common.h

40
legacy/main-old.cc

@ -2,51 +2,11 @@
/* /*
uint64_t -> 0000 + [xxx] * 20
2x2 2x1 1x2 1x1
# # # # # #
# # #
00 01 02 03
04 05 06 07
08 09 10 11
12 13 14 15
16 17 18 19
x1 (%4) => 0 1 2 3 x1 (%4) => 0 1 2 3
x3 (%4) => 0 3 2 1 x3 (%4) => 0 3 2 1
*/ */
/*
======================================================
1x1 -> 011 000 000 000 -> 0000 0000 0011 -> 0x3
1x2 -> 001 111 000 000 -> 0000 0011 1001 -> 0x39
2x1 -> 010 000 000 000 -> 0000 0000 0010 -> 0x7002
111 000 000 000 -> 0000 0000 0111
2x2 -> 100 111 000 000 -> 0000 0011 1100 -> 0x3F03C
111 111 000 000 -> 0000 0011 1111
======================================================
1x1 -> 111 000 000 000 -> 0000 0000 0111 -> 0x7
1x2 -> 111 111 000 000 -> 0000 0011 1111 -> 0x3F
2x1 -> 111 000 000 000 -> 0000 0000 0111 -> 0x7007
111 000 000 000 -> 0000 0000 0111
2x2 -> 111 111 000 000 -> 0000 0011 1111 -> 0x3F03F
111 111 000 000 -> 0000 0011 1111
======================================================
*/
int main() { int main() {
printf("Klotski engine\n"); printf("Klotski engine\n");

13
src/klotski/utils/common.cc

@ -7,7 +7,7 @@ uint32_t Common::range_reverse(uint32_t bin) { // reverse binary every 2-bits
return ((bin << 2) & 0xCCCCCCCC) | ((bin >> 2) & 0x33333333); return ((bin << 2) & 0xCCCCCCCC) | ((bin >> 2) & 0x33333333);
} }
/// NOTE: don't check unknown ranges /// WARN: don't check unknown data -> may cause infinite loop
uint8_t Common::check_range(uint32_t head, uint32_t range) { // check generated range uint8_t Common::check_range(uint32_t head, uint32_t range) { // check generated range
/// M_1x1 M_1x2 M_2x1 M_2x2 /// M_1x1 M_1x2 M_2x1 M_2x2
/// 1 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0 /// 1 0 0 0 1 1 0 0 1 0 0 0 1 1 0 0
@ -18,25 +18,24 @@ uint8_t Common::check_range(uint32_t head, uint32_t range) { // check generated
constexpr uint32_t M_2x1 = 0b10001; constexpr uint32_t M_2x1 = 0b10001;
constexpr uint32_t M_2x2 = 0b110011; constexpr uint32_t M_2x2 = 0b110011;
int block_offset = 1; uint8_t block_offset = 1;
uint32_t cache = M_2x2 << head; // fill 2x2 block uint32_t cache = M_2x2 << head; // fill 2x2 block
for (int addr = 0; range; range >>= 2, ++block_offset) { // traverse every 2-bits for (int addr = 0; range; range >>= 2, ++block_offset) { // traverse every 2-bits
// TODO: will addr >= 32 in some special cases?
while ((cache >> addr) & 0b1) { while ((cache >> addr) & 0b1) {
++addr; // search next unfilled block ++addr; // search next unfilled block
} }
switch (range & 0b11) { switch (range & 0b11) {
case 0b00: // space case 0b00: /// space
case 0b11: // 1x1 block case 0b11: /// 1x1 block
cache |= M_1x1 << addr; // fill space or 1x1 block cache |= M_1x1 << addr; // fill space or 1x1 block
break; break;
case 0b10: // 2x1 block case 0b10: /// 2x1 block
if (addr > 15 || cache >> (addr + 4) & 0b1) { // invalid address if (addr > 15 || cache >> (addr + 4) & 0b1) { // invalid address
return block_offset; // broken block number return block_offset; // broken block number
} }
cache |= M_2x1 << addr; // fill 2x1 block cache |= M_2x1 << addr; // fill 2x1 block
break; break;
case 0b01: // 1x2 block case 0b01: /// 1x2 block
if ((addr & 0b11) == 0b11 || cache >> (addr + 1) & 0b1) { // invalid address if ((addr & 0b11) == 0b11 || cache >> (addr + 1) & 0b1) { // invalid address
return block_offset; // broken block number return block_offset; // broken block number
} }

4
src/klotski/utils/common.h

@ -9,6 +9,10 @@
/// ///
/// 3. `2x2` block must have and only one, `2x1` `1x2` `1x1` are not required in number. /// 3. `2x2` block must have and only one, `2x1` `1x2` `1x1` are not required in number.
/// ///
/// NOTE: 2x2 2x1 1x2 1x1
/// # # # # # #
/// # # #
///
/// After statistics, there are a total of 29334498 cases that meet the above requirements. /// After statistics, there are a total of 29334498 cases that meet the above requirements.
#include <cstdint> #include <cstdint>

Loading…
Cancel
Save