-
-
Save poying/4118421 to your computer and use it in GitHub Desktop.
ch6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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