Advertisement
splash365

N-queen

Nov 8th, 2020
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.14 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define     rep(i,k,n)      for(long long int i=k;i<n;i++)
  5. #define     fast            ios_base::sync_with_stdio(false);cin.tie(NULL)
  6. #define     read            freopen("input.txt","r",stdin)
  7. #define     write           freopen("output.txt","w",stdout)
  8. #define     D(x)            cout << '>' << #x << ':' << x << endl
  9. #define     DD(x,y)         cout << '>' << #x << ':' << x << ' ' << #y << ':' << y << endl
  10. #define     DDD(x,y,z)      cout << '>' << #x << ':' << x << ' ' << #y << ':' << y << ' ' << #z << ':' << z << endl
  11. #define     PI              acos(-1)
  12. #define     MP(x, y)        make_pair(x, y)
  13. #define     PB(x)           push_back(x)
  14. #define     ALL(p)          p.begin(),p.end()
  15. #define     CLR(p)          memset(p, 0, sizeof(p))
  16. #define     MEM(a, b)       memset(a, (b), sizeof(a))
  17. #define     ff              first
  18. #define     ss              second
  19. #define     sf              scanf
  20. #define     pf              printf
  21. #define     PII             pair<int, int>
  22. #define     ll              long long int
  23. #define     ull             unsigned long long int
  24.  
  25. inline int two(int n) { return 1 << n; }
  26. inline int test(int n, int b) { return (n>>b)&1; }
  27. inline void set_bit(int & n, int b) { n |= two(b); }
  28. inline void unset_bit(int & n, int b) { n &= ~two(b); }
  29. inline int last_bit(int n) { return n & (-n); }
  30. inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }
  31.  
  32. template < class T > inline T gcd(T a, T b) {while(b) { a %= b; swap(a, b); } return a;}
  33. template < class T > inline T bigmod(T p, T e, T M){
  34.     ll ret = 1;
  35.     for(; e > 0; e >>= 1){ if(e & 1) ret = (ret * p) % M; p = (p * p) % M; }
  36.     return (T)ret;
  37. }
  38. template < class T > inline T power(T a, T n) {
  39.     if(n==0) return 1;
  40.     if(n==1) return a;
  41.     T R = power(a,n/2);
  42.     return (n%2==0) ? R*R : R*a*R;
  43. }
  44.  
  45. int fx4[] = {0, 0, -1, +1};
  46. int fy4[] = {+1, -1, 0, 0};
  47. int fx8[] = {1, 1, 0, -1, -1, -1, 0, 1, 0};
  48. int fy8[] = {0, 1, 1, 1, 0, -1, -1, -1, 0};
  49. int fx8Knight[] = {+2, +2, +1, -1, -2, -2, -1, +1};
  50. int fy8Knight[] = {+1, -1, -2, -2, -1, +1, +2, +2};
  51.  
  52. const int MAXN = 107;
  53.  
  54. bool board[MAXN][MAXN];
  55. int N, cnt;
  56.  
  57. void print()
  58. {
  59.     rep(i, 0, N)
  60.     {
  61.         rep(j,0,N)
  62.         {
  63.             cout << (int)board[i][j] << ' ';
  64.         }
  65.         cout << "\n";
  66.     }
  67.     cout << "\n\n";
  68. }
  69.  
  70. bool check(int x, int y)
  71. {
  72.     for (int i = x; i >= 0; i--) if(board[i][y]) return false;
  73.     for (int i = x, j = y; i >= 0 && j >= 0; i--, j--) if(board[i][j]) return false;
  74.     for (int i = x, j = y; i >= 0 && j < N; i--, j++) if(board[i][j]) return false;
  75.     return true;
  76. }
  77.  
  78. void solve(int i)
  79. {
  80.     if(i == N)
  81.     {
  82.         cnt++;
  83.         print();
  84.         return;
  85.     }
  86.  
  87.     for (int j = 0; j < N; j++)
  88.     {
  89.         if(check(i, j))
  90.         {
  91.             board[i][j] = true;
  92.             solve(i + 1);
  93.             board[i][j] = false;
  94.         }
  95.     }
  96. }
  97.  
  98.  
  99. int main() {
  100. #ifndef ONLINE_JUDGE
  101.     read;
  102.     write;
  103. #endif
  104.     fast;
  105.     MEM(board, false);
  106.     cnt = 0;
  107.     cin >> N;
  108.     solve(0);
  109.     cout << "Total: " << cnt << endl;
  110.     return 0;
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement