Skip to content

Instantly share code, notes, and snippets.

@x1024
Created May 24, 2011 13:30
Show Gist options
  • Save x1024/988699 to your computer and use it in GitHub Desktop.
Save x1024/988699 to your computer and use it in GitHub Desktop.
BitTorrent task solution
#include<stdio.h>
#define BOWLS 40
#define 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;
int y;
int tmp_index;
void check(int x) {
if (turn == ORANGES) {
++count;
return;
}
for(; x < BOWLS; ++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;
++turn;
check(x+1);
--turn;
// 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;
}
}
}
int main() {
check(0);
printf("%d\n", count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment