[关闭]
@zwh8800 2017-08-23T02:24:08.000000Z 字数 578 阅读 190980

一个八皇后解法

blog 归档 八皇后 算法


一个八皇后解法


  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4. #define QUEEN 8
  5. int board[QUEEN] = {0};
  6. void Print()
  7. {
  8. static int count = 0;
  9. cout << "Solve #" << ++count << ':' << endl;
  10. for (int line = 0; line < QUEEN; ++line)
  11. {
  12. for (int col = 0; col < QUEEN; ++col)
  13. {
  14. if (board[line] == col)
  15. cout << "●";
  16. else
  17. cout << "○";
  18. }
  19. cout << endl;
  20. }
  21. cout << endl;
  22. }
  23. bool Check(int line)
  24. {
  25. for (int y = 0; y < line; ++y)
  26. if (board[y] == board[line] || abs(board[y] - board[line]) == abs(y - line))
  27. return false;
  28. return true;
  29. }
  30. void Put(int line)
  31. {
  32. if (line >= QUEEN)
  33. {
  34. Print();
  35. return;
  36. }
  37. for (int col = 0; col < QUEEN; ++col)
  38. {
  39. board[line] = col;
  40. if (Check(line))
  41. Put(line + 1);
  42. }
  43. }
  44. int main()
  45. {
  46. Put(0);
  47. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注