Advertisement
newb_ie

Untitled

Sep 29th, 2021
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
  5. int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
  6.  
  7. bool inside (int i,int j,int n) {
  8. if (i >= 0 and i < n and j >= 0 and j < n) return true;
  9. return false;
  10. }
  11.  
  12. void next (vector<string> &grid,int i,int j,int delta_i,int delta_j,int n) {
  13. if (inside(i,j,n) == false) return;
  14. grid[i][j] = 'x';
  15. next(grid,i + delta_i,j + delta_j,delta_i,delta_j,n);
  16. }
  17.  
  18. vector<vector<string>> res;
  19.  
  20. void f (vector<string> grid,int y,int n) {
  21. if (y == n) {
  22. res.push_back(grid);
  23. return;
  24. }
  25. for (int x = 0; x < n; ++x) {
  26. if (grid[x][y] == '.') {
  27. vector<string> grid_copy = grid;
  28. for (int i = 0; i < 8; ++i) {
  29. next(grid_copy,x,y,dx[i],dy[i],n);
  30. }
  31. grid_copy[x][y] = 'Q';
  32. f (grid_copy,y + 1,n);
  33. }
  34. }
  35. }
  36.  
  37. int main () {
  38. ios::sync_with_stdio(false);
  39. cin.tie(nullptr);
  40. cout.tie(nullptr);
  41. int n;
  42. cin >> n;
  43. vector<string> grid;
  44. for (int i = 0; i < n; ++i) grid.push_back(string(n,'.'));
  45. f(grid,0,n);
  46. for (int p = 0; p < (int) res.size(); ++p) {
  47. for (int i = 0; i < n; ++i) {
  48. cout << res[p][i] << '\n';
  49. }
  50. cout << '\n';
  51. }
  52. }
  53.  
  54.  
  55. //~ HW :
  56. //~ 1) print 1 to n using recursion
  57. //~ 2) print n to 1 ...............
  58. //~ 3) sum using ..................
  59. //~ 4) fibonacchi using ...........
  60. //~ 5) factorial using ............
  61. //~ 6) reverse printing using .....
  62.  
  63. //~ s,t :
  64. //~ s = "sfjsldfjsdf",t = "ssfsfd"
  65. //~ bfs(),dfs(),disjoint set union
  66. //~ dp(basic)
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement