fork(2) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define FAST_IO ios::sync_with_stdio(0); cin.tie(0);
  4.  
  5. int N;
  6. bool board[14][14];
  7.  
  8. bool check_can_place(int y, int x)
  9. {
  10. // 열 체크
  11. for (int i = 0;i<N;++i)
  12. if (board[i][x])
  13. return false;
  14.  
  15. // 우상좌하 대각선 체크
  16. for (int i=0;i<N;++i)
  17. {
  18. const int j=y+x-i;
  19. if (j >= N)
  20. continue;
  21. if (board[i][j])
  22. return false;
  23. }
  24.  
  25. // 좌상우하 대각선 체크
  26. for (int i=0;i<N;++i)
  27. {
  28. const int j=-y+x+i;
  29. if (j < 0)
  30. continue;
  31. if (board[i][j])
  32. return false;
  33. }
  34.  
  35. return true;
  36. }
  37.  
  38. int solve(int i = 0)
  39. {
  40. if (i == N)
  41. return 1;
  42.  
  43. int ret = 0;
  44. for (int j=0;j<N;++j)
  45. {
  46. if (check_can_place(i, j))
  47. {
  48. board[i][j] = true;
  49. ret += solve(i + 1);
  50. board[i][j] = false;
  51. }
  52. }
  53.  
  54. return ret;
  55. }
  56.  
  57.  
  58. int main()
  59. {
  60. FAST_IO;
  61. cin >> N;
  62. cout << solve();
  63. return 0;
  64. }
Success #stdin #stdout 2.42s 4532KB
stdin
14
stdout
70456