Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- int x[8]; /* x[i] = x coordinate of queen in row i. */
- /* prints field */
- void print ()
- {
- int i,j;
- printf ("+-----------------------+\n");
- for (i=0; i<8; i++) {
- printf ("|");
- for (j=0; j<8; j++)
- if (j==x[i]) printf ("Q.|");
- else printf (" |");
- printf ("\n");
- printf ("+-----------------------+\n");
- }
- printf ("\n");
- }
- /* tests, whether (ix, iy) is beaten by queens 0...(iy-1) */
- bool is_free (int ix, int iy)
- {
- int i;
- for (i=0; i<iy; i++)
- if ((x[i]==ix) || (abs(x[i]-ix)==abs(i-iy)))
- return false;
- return true;
- }
- /* tries to place queen n on row n */
- void placequeen (int n)
- {
- int i;
- if (n==8)
- print();
- else
- for (i=0; i<8; i++)
- if (is_free(i,n)) {
- x[n]=i;
- placequeen (n+1);
- }
- }
- int main ()
- {
- placequeen (0);
- return 0;
- }
Add Comment
Please, Sign In to add comment