#include #include #define SIZE 5 #define ORIENTATIONS (1 << SIZE) int Sequence[2 * SIZE]; int Sequence_Limit = 1; int Numbers[2 * SIZE][2][SIZE] = { { /* 0 */ { 7, 50, 68, 85, 61 }, { 39, 59, 14, 16, 52 }, }, { /* 1 */ { 26, 44, 90, 84, 38 }, { 28, 11, 20, 13, 8 }, }, { /* 2 */ { 67, 67, 79, 96, 12 }, { 37, 25, 6, 95, 3 }, }, { /* 3 */ { 52, 68, 55, 54, 62 }, { 68, 55, 32, 46, 83 }, }, { /* 4 */ { 97, 29, 89, 86, 44 }, { 35, 54, 21, 45, 4 }, }, { /* 5 */ { 15, 63, 94, 15, 40 }, { 68, 7, 66, 4, 63 }, }, { /* 6 */ { 95, 71, 60, 49, 58 }, { 89, 22, 19, 23, 28 }, }, { /* 7 */ { 58, 46, 80, 84, 52 }, { 30, 7, 7, 91, 39 }, }, { /* 8 */ { 80, 72, 2, 37, 62 }, { 17, 26, 82, 35, 44 }, }, { /* 9 */ { 61, 6, 30, 45, 76 }, { 23, 3, 4, 37, 61 }, }, }; void InitSequence (void) { int i; for (i = 0; i < (2 * SIZE); ++i) { Sequence[i] = i; Sequence_Limit *= (i + 1); } Sequence_Limit /= 2; } inline int NextSequence (void) { static int count = 1; int i = (2*SIZE) - 1; int mask = 0; int seq = 0; int j; if (count == Sequence_Limit) { return 0; } while ((Sequence[i] < Sequence[i - 1]) && (i > 0)) { i -= 1; } i -= 1; if (i < 0) { return 0; } for (j = 0; j < i; ++j) { mask |= 1 << Sequence[j]; } do { Sequence[i] += 1; } while ((mask & (1 << Sequence[i])) != 0); mask |= 1 << Sequence[i++]; while (i < (2*SIZE)) { while ((mask & 0x1) != 0) { mask >>= 1; seq += 1; } Sequence[i++] = seq; mask >>= 1; seq += 1; } count += 1; if ((count % 10000) == 0) { printf("%d\n", count); } return 1; } int main (int argc, char* argv[]) { int ro, co, c, r; int nrt, nrb, nct, ncb; int diff; int index; int max = 0; InitSequence(); do { for (co = 0; co < (ORIENTATIONS / 2); ++co) { for (ro = 0; ro < ORIENTATIONS; ++ro) { diff = 0; for (c = 0; c < SIZE; ++c) { for (r = 0; r < SIZE; ++r) { index = (ro & (1 << r)) > 0 ? (SIZE - c - 1) : c; assert(index >= 0); assert(index < SIZE); assert(Sequence[r] < (2*SIZE)); nrt = Numbers[Sequence[r]][0][index]; nrb = Numbers[Sequence[r]][1][index]; index = (co & (1 << c)) > 0 ? (SIZE - r - 1) : r; assert(index >= 0); assert(index < SIZE); assert(Sequence[c + SIZE] < (2*SIZE)); nct = Numbers[Sequence[c + SIZE]][0][index]; ncb = Numbers[Sequence[c + SIZE]][1][index]; if ((nrt - ncb) > (nct - nrb)) { diff += nrt - ncb; } else { diff += nct - nrb; } } } if (diff >= max) { max = diff; printf("Nex max: %d\n", max); printf("Sequence: "); for (r = 0; r < SIZE; ++r) { printf("%3d(%s)", Sequence[r], (ro & (1 << r)) > 0 ? "F" : "N"); } for (c = 0; c < SIZE; ++c) { printf("%3d(%s)", Sequence[c + SIZE], (co & (1 << c)) > 0 ? "F" : "N"); } printf("\n"); printf("Top Grid:\n"); for (c = 0; c < SIZE; ++c) { for (r = 0; r < SIZE; ++r) { index = (ro & (1 << r)) > 0 ? (SIZE - c - 1) : c; nrt = Numbers[Sequence[r]][0][index]; nrb = Numbers[Sequence[r]][1][index]; index = (co & (1 << c)) > 0 ? (SIZE - r - 1) : r; nct = Numbers[Sequence[c + SIZE]][0][index]; ncb = Numbers[Sequence[c + SIZE]][1][index]; if ((nrt - ncb) > (nct - nrb)) { printf("%3dr", nrt); } else { printf("%3dc", nct); } } printf("\n"); } printf("Bottom Grid:\n"); for (c = 0; c < SIZE; ++c) { for (r = 0; r < SIZE; ++r) { index = (ro & (1 << r)) > 0 ? (SIZE - c - 1) : c; nrt = Numbers[Sequence[r]][0][index]; nrb = Numbers[Sequence[r]][1][index]; index = (co & (1 << c)) > 0 ? (SIZE - r - 1) : r; nct = Numbers[Sequence[c + SIZE]][0][index]; ncb = Numbers[Sequence[c + SIZE]][1][index]; if ((nrt - ncb) > (nct - nrb)) { printf("%3dc", ncb); } else { printf("%3dr", nrb); } } printf("\n"); } printf("\n\n"); } } } } while (NextSequence()); printf("%d\n", (Sequence_Limit * ORIENTATIONS * (ORIENTATIONS / 2) * SIZE * SIZE)); }