Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};
- int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};
- bool inside (int i,int j,int n) {
- if (i >= 0 and i < n and j >= 0 and j < n) return true;
- return false;
- }
- void next (vector<string> &grid,int i,int j,int delta_i,int delta_j,int n) {
- if (inside(i,j,n) == false) return;
- grid[i][j] = 'x';
- next(grid,i + delta_i,j + delta_j,delta_i,delta_j,n);
- }
- vector<vector<string>> res;
- void f (vector<string> grid,int y,int n) {
- if (y == n) {
- res.push_back(grid);
- return;
- }
- for (int x = 0; x < n; ++x) {
- if (grid[x][y] == '.') {
- vector<string> grid_copy = grid;
- for (int i = 0; i < 8; ++i) {
- next(grid_copy,x,y,dx[i],dy[i],n);
- }
- grid_copy[x][y] = 'Q';
- f (grid_copy,y + 1,n);
- }
- }
- }
- int main () {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int n;
- cin >> n;
- vector<string> grid;
- for (int i = 0; i < n; ++i) grid.push_back(string(n,'.'));
- f(grid,0,n);
- for (int p = 0; p < (int) res.size(); ++p) {
- for (int i = 0; i < n; ++i) {
- cout << res[p][i] << '\n';
- }
- cout << '\n';
- }
- }
- //~ HW :
- //~ 1) print 1 to n using recursion
- //~ 2) print n to 1 ...............
- //~ 3) sum using ..................
- //~ 4) fibonacchi using ...........
- //~ 5) factorial using ............
- //~ 6) reverse printing using .....
- //~ s,t :
- //~ s = "sfjsldfjsdf",t = "ssfsfd"
- //~ bfs(),dfs(),disjoint set union
- //~ dp(basic)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement