Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int n, a[101], f[101];
- int check() {
- for(int i = 0; i < n; i++)
- if(a[i] == i+1) return 1;
- return 0;
- }
- void backtrack(int index) {
- if(index == n) {
- if(check()) {
- for(int i = 0; i < n; i++)
- cout << a[i] << " ";
- cout << endl;
- }
- return;
- }
- for(int i = 1; i <= n; i++) {
- if(f[i] == 0) {
- a[index] = i;
- f[i] = 1;
- backtrack(index + 1);
- f[i] = 0;
- }
- }
- }
- int main() {
- cin >> n;
- backtrack(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement