Advertisement
thienlang

Đặt Hậu

May 1st, 2013
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. #include<stdio.h>
  2. #include "iostream"
  3. #include "conio.h"
  4. #include "fstream"
  5. using namespace std;
  6. int N;
  7. fstream file;
  8. void NhapHau( int &N)
  9. {
  10.     cout << "Nhap So Quan Hau Can Dat: ";
  11.     cin >> N;
  12.     file << N << endl;
  13. }
  14.  
  15. short* x = new short[N];
  16.  
  17. void VietNghiem()
  18. {
  19.     static short i = 1;
  20.     short j;
  21.     file << "Cach thu " << i++ << endl;
  22.     int k;
  23.     for( j = 0; j < N; j++)
  24.         file<<"["<<j+1<<"]"<<"["<<x[j]+1<<"]"<<" "<< endl;
  25. }
  26.  
  27. short KiemTra( short i )
  28. {
  29.     short j;
  30.     for( j = 0 ; j < i ; j++) if( x[i] == x[j] || i - x[i] == j - x[j] || i + x[i] == j + x[j]) return 0;
  31.     return 1;
  32. }
  33.  
  34. void XepHau(short i )
  35. {
  36.     short j;
  37.     if( i == N )
  38.         VietNghiem();
  39.     else
  40.         for( j = 0 ; j < N ; j++)
  41.         {
  42.             x[i] = j;
  43.             if( KiemTra( i))
  44.                 XepHau( i+1);
  45.         }
  46. }
  47.  
  48. void main()
  49. {
  50.     file.open("DeQuy_DatHau.txt", ios::out);
  51.     NhapHau(N);
  52.     XepHau( 0);
  53.    
  54.     file.close();
  55.     delete []x;
  56.    
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement