All pastes #798828 Raw Edit

Untitled

public text v1 · immutable
#798828 ·published 2007-11-27 01:20 UTC
rendered paste body
int sols,width;
int colbad[100], diagbadleft[100], diagbadright[100];

void placequeen(int row, int *moves) {
  if(row == width) {
    if(sols<3) {
      FILE *fout = fopen ("checker.out", "a");
      int i;
      for(i = 0; i < width; i++) {
        if(i != width -1 ) fprintf(fout,"%d ", moves[i]+1);
        else fprintf(fout,"%d\n",moves[i]+1);
      }
      fclose(fout);
    }
    sols++;
    return;
  }
  int j;
  for(j = 0; j < width; j++) {
    if(!(colbad[j] || diagbadleft[row+j] || diagbadright[j-row+width])) {
      colbad[j] = 1;
      moves[row] = j;
      diagbadleft[row+j] = 1;
      diagbadright[j-row+width] = 1;
      placequeen(row+1,moves);
      moves[row] = 0;
      diagbadleft[row+j] = 0;
      diagbadright[j-row+width] = 0;
      colbad[j] = 0;
    }
  }
}

int main() {
  FILE *fin  = fopen ("checker.in", "r");
  fscanf (fin, "%d\n", &width);
  int moves[width];
  placequeen(0, moves);
  FILE *fout = fopen ("checker.out", "a");
  fprintf (fout, "%d\n", sols);
  return 0;
}