Skip to content

Instantly share code, notes, and snippets.

@poying
Created November 20, 2012 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save poying/4118421 to your computer and use it in GitHub Desktop.
Save poying/4118421 to your computer and use it in GitHub Desktop.
ch6
#include <stdio.h>
#include <stdlib.h>
int counter = 0;
int abs(int val){
return val < 0? val * -1: val;
}
void draw(int * array, int size){
int i;
for(i = 0; i < size; i++){
printf("%d\t", *(array + i));
}
printf("\n");
}
void check(int size, int time, int * position){
int i, j, ok = 1, * copy;
for(i = 0; i < size; i++){
ok = 1;
for(j = 0; j < size - time; j++){
if(abs(i - *(position + j)) == abs(size - time - j) || *(position + j) == i){
ok = 0;
}
}
if(ok){
copy = malloc(sizeof(int) * size);
for(j = 0; j < size; j++){
*(copy + j) = *(position + j);
}
*(copy + size - time) = i;
if(time == 1){
draw(copy, size);
counter++;
}else{
check(size, time - 1, copy);
}
free(copy);
}
}
}
void run(int size){
int position[size], i;
for(i = 0; i < size; i++){
position[i] = -1;
}
check(size, size, position);
}
void main(){
run(8);
printf("%d\n", counter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment