8q.c 526 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. int up[15], down[15], rows[8], x[8];
  2. int queens(), print();
  3. main()
  4. {
  5. int i;
  6. for (i = 0; i < 15; i++)
  7. up[i] = down[i] = 1;
  8. for (i = 0; i < 8; i++)
  9. rows[i] = 1;
  10. queens(0);
  11. return 0;
  12. }
  13. queens(c)
  14. {
  15. int r;
  16. for (r = 0; r < 8; r++)
  17. if (rows[r] && up[r-c+7] && down[r+c]) {
  18. rows[r] = up[r-c+7] = down[r+c] = 0;
  19. x[c] = r;
  20. if (c == 7)
  21. print();
  22. else
  23. queens(c + 1);
  24. rows[r] = up[r-c+7] = down[r+c] = 1;
  25. }
  26. }
  27. print()
  28. {
  29. int k;
  30. for (k = 0; k < 8; k++)
  31. printf("%c ", x[k]+'1');
  32. printf("\n");
  33. }