1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
#include <iostream>
#include <array>
typedef std::array<bool, 40> mask_t;
typedef std::array<int, 20> indices_t;
const int actions[][4] = {
{-1, -1, -1, -1}, //0
{-1, -1, -1, -1}, //1
{ 0, 1, -1, -1}, //2
{-1, -1, -1, -1}, //3
{ 0, 3, -1, -1}, //4
{-1, -1, -1, -1}, //5
{ 2, 5, -1, -1}, //6
{-1, -1, -1, -1}, //7
{ 3, 7, 5, -1}, //8
{-1, -1, -1, -1}, //9
{ 1, 7, 9, -1}, //10
{ 4, 10, -1, -1}, //11
{-1, -1, -1, -1}, //12
{ 2, 8, 11, 12}, //13
{ 3, 9, 13, -1}, //14
{11, 14, -1, -1}, //15
{ 6, 15, -1, -1}, //16
{ 1, 8, 16, -1}, //17
{10, 13, 16, -1}, //18
};
const int print_order[] = {0, 1, 2, 3, 7, 8, 5, 4, 9, 12, 17, 6, 10, 13, 18, 16, 11, 14, 15};
void loop(int level, mask_t &mask, indices_t &indices);
void loop2(int level, mask_t &mask, indices_t &indices){
auto i = indices[level] - 1;
if (i < 0 || !mask[i])
return;
mask[i] = false;
loop(level + 1, mask, indices);
mask[i] = true;
}
void loop(int level, mask_t &mask, indices_t &indices){
if (level >= 19){
for (auto i : print_order)
std::cout << indices[i] << ' ';
std::cout << std::endl;
return;
}
auto &a = actions[level];
if (a[0] < 0){
for (int i = 1; i <= 20; i++){
indices[level] = i;
loop2(level, mask, indices);
}
}else{
int i = 38;
for (auto index : a)
if (index >= 0)
i -= indices[index];
indices[level] = i;
loop2(level, mask, indices);
}
}
int main(){
mask_t mask;
for (int i = 0; i < 40; i++)
mask[i]= i < 20;
indices_t indices;
loop(0, mask, indices);
return 0;
}
| |