pacozaa

Code2

Feb 4th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. int x[8]; /* x[i] = x coordinate of queen in row i. */
  5. /* prints field */
  6. void print ()
  7. {
  8. int i,j;
  9. printf ("+-----------------------+\n");
  10. for (i=0; i<8; i++) {
  11. printf ("|");
  12. for (j=0; j<8; j++)
  13. if (j==x[i]) printf ("Q.|");
  14. else printf (" |");
  15. printf ("\n");
  16. printf ("+-----------------------+\n");
  17. }
  18. printf ("\n");
  19. }
  20. /* tests, whether (ix, iy) is beaten by queens 0...(iy-1) */
  21. bool is_free (int ix, int iy)
  22. {
  23. int i;
  24. for (i=0; i<iy; i++)
  25. if ((x[i]==ix) || (abs(x[i]-ix)==abs(i-iy)))
  26. return false;
  27. return true;
  28. }
  29. /* tries to place queen n on row n */
  30. void placequeen (int n)
  31. {
  32. int i;
  33. if (n==8)
  34. print();
  35. else
  36. for (i=0; i<8; i++)
  37. if (is_free(i,n)) {
  38. x[n]=i;
  39. placequeen (n+1);
  40. }
  41. }
  42. int main ()
  43. {
  44. placequeen (0);
  45. return 0;
  46. }
Add Comment
Please, Sign In to add comment