Advertisement
nex036ara

queens_vector

Mar 12th, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <stdlib.h>
  4. using namespace std;
  5.  
  6.  
  7. int brResenja=0;
  8. int n;
  9. int e=0; //poziv kraljice(0)
  10. int i=1; //poziv dozvola(0)
  11.  
  12. static vector<int> kolone; //cuvaju indeks pozicije kraljice
  13. void kraljice(int e); //pretraga resenja
  14. bool dozvola(int i); //provera dozvole
  15.  
  16.  
  17. int main(int argc, char *argv[]) {
  18.    
  19.  
  20.     cout<<"Unesi dimenzije tabele(n): "<<endl;
  21.     cin>>n;
  22.     if(n>0)
  23.     {
  24.        
  25.         for(int a=0; a<n; a++) {
  26.         kolone.push_back(0);
  27.         }
  28.     kraljice(e);
  29.     cout<<"Broj resenja: "<<brResenja<<endl;
  30.     }
  31. return 0;
  32. }
  33.  
  34.  
  35. void kraljice(int i) {  
  36.     if(dozvola(i-1)) {  
  37.         if(i==n) brResenja++;
  38.         else {
  39.         for(int l=0; l<n; l++){
  40.             kolone[i]= l;
  41.         kraljice(i+1);
  42.         }
  43.     }
  44. }
  45. }
  46. bool dozvola(int i) {
  47.     for(int s=0; s<i; s++)
  48.         if(kolone[i]==kolone[s] || abs(kolone[i] -kolone[s])== i-s) return false;
  49.     return true;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement