Skip to content

Instantly share code, notes, and snippets.

@x1024
Created May 24, 2011 17:42
Show Gist options
  • Save x1024/989227 to your computer and use it in GitHub Desktop.
Save x1024/989227 to your computer and use it in GitHub Desktop.
BitTorrent task solution
#include<stdio.h>
const int BOWLS = 40;
const int ORANGES = 9;
int count = 0;
// All the places in which an orange can't be placed are 1's
int used[BOWLS];
// A list of the oranges already placed.
int oranges[ORANGES];
int turn = 0;
int y;
int tmp_index;
template <int turn>
inline void check(int x) {
if (turn == ORANGES) {
++count;
return;
}
for(; x < BOWLS - (ORANGES - turn - 1); ++x) {
// Only check available positions.
if (!used[x]) {
// Mark this position as used.
used[x] += 1;
// This position is called "B"
// For every A, make sure no C exists such that |A-B| = |B-C|
// I.e. mark them as used even though no oranges are placed there.
for(y = 0; y < turn; ++y) {
tmp_index = x + x - oranges[y];
if (tmp_index >= 0 && tmp_index < BOWLS) {
used[tmp_index] += 1;
}
}
// The orange on this turn is placed on "x"
oranges[turn] = x;
check<turn+1>(x+1);
// Revert the "used" markers placed this turn.
for(y = 0; y < turn; ++y) {
tmp_index = x + x - oranges[y];
if (tmp_index >= 0 && tmp_index < BOWLS) {
used[tmp_index] -= 1;
}
}
used[x] -= 1;
}
}
}
template <>
inline void check<ORANGES>(int x) {
++count;
}
int main() {
check<0>(0);
printf("%d\n", count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment