Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<math.h>
- using namespace std;
- int a[20];
- int count;
- bool Ok(int x2,int y2){
- for(int i = 1; i < x2 ;i++)
- if(a[i] == y2 || abs(i-x2) == abs(a[i] - y2) )
- return false;
- return true;
- }
- void Xuat(int n){
- cout << endl << ++count << ": ";
- for(int i = 1;i <= n;i++)
- cout << a[i] << " ";
- cout << endl;
- }
- void Try(int i,int n){
- for(int j = 1;j <= n;j++){
- if(Ok(i,j)){
- a[i] = j;
- if(i == n) Xuat(n);
- Try(i+1,n);
- }
- }
- }
- int main(){
- cout << "Nhap n: ";
- int n;
- cin >> n;
- cout << endl << "----- Cac cach sap xep: ----- " << endl;
- Try(1,n);
- if (count == 0) cout << "----- Khong co cach sap xep nao! -----" << endl;
- cout << endl << "---- Co " << count << " cach sap xep cac con hau! ----" << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment