Advertisement
arif334

UVa 729

Jan 6th, 2024
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.23 KB | None | 0 0
  1. /**
  2.     * Author    : Arif Ahmad
  3.     * Date      : 30-08-14
  4.     * Algo      : Backtracking + Pruning
  5.     * Difficulty: Medium
  6. */
  7.  
  8. #include <bits/stdc++.h>
  9. using namespace std;
  10.  
  11. #define INF_MAX     2147483647
  12. #define INF_MIN     -2147483648
  13. #define INF         (1 << 30)
  14. #define EPS         1e-9
  15. #define PI          acos(-1.0)
  16. #define N           2 + 16
  17. #define MOD         10000000007
  18. #define sz(x)       (int)(x).size()
  19. #define all(x)      (x).begin(), (x).end()
  20. #define pb          push_back
  21. #define mp          make_pair
  22. #define ms(x, a)    memset((x), (a), sizeof(x))
  23. #define F           first
  24. #define S           second
  25. #define rep(i,a,b)  for(int i=(a); i<(b); i++)
  26. #define repC(i,x)   for(size_t i=0; i<x.size(); i++)
  27. #define repIT(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
  28.  
  29. typedef long long       LL;
  30. typedef pair<int,int>   pii;
  31. typedef vector<int>     vi;
  32. typedef vector<string>  vs;
  33. typedef vector<char>    vc;
  34. typedef vector<bool>    vb;
  35. typedef map<string,int> msi;
  36. typedef map<int,int>    mii;
  37. typedef map<char,int>   mci;
  38. typedef map<int,string> mis;
  39.  
  40. template<class T> T Abs(T x) {return x>0 ? x : -x;}
  41. template<class T> T Max(T a, T b) {return a>b ? a : b;}
  42. template<class T> T Min(T a, T b) {return a<b ? a : b;}
  43. template<class T> T gcd(T a, T b) {return (b ? gcd(b,a%b) : a);}
  44. bool isVowel(char ch){ch=tolower(ch);return(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u');}
  45.  
  46. int n, h;
  47. char res[20];
  48. string s;
  49. bool taken[20];
  50.  
  51. void backtrack(int i)
  52. {
  53.     if(i == n) {
  54.         res[i] = '\0';
  55.         puts(res);
  56.         return;
  57.     }
  58.    
  59.     for(int j = 0; j < n; j++) if(!taken[j]) {
  60.         taken[j] = true;
  61.         res[i] = s[j];
  62.         backtrack(i+1);
  63.         taken[j] = false;
  64.        
  65.         while(s[j+1] == s[j]) j++; // pruning
  66.     }
  67. }
  68.  
  69. int main()
  70. {
  71.     #ifndef ONLINE_JUDGE
  72.         freopen("in.txt", "r", stdin);
  73.     #endif
  74.  
  75.     int i, j, k, tc;
  76.  
  77.     scanf("%d", &tc);
  78.     rep(cn, 1, tc+1)
  79.     {
  80.         scanf("%d%d", &n, &h);
  81.  
  82.         s = "";
  83.         for(i = 1; i <= (n - h); i++) s += "0";
  84.         for(i = 1; i <= h; i++) s += "1";
  85.        
  86.         if(cn > 1) cout << "\n";
  87.        
  88.         //memset(taken, false, sizeof(taken));
  89.         //backtrack(0);
  90.        
  91.         // 2nd approach - using next_permutation
  92.         do{
  93.             cout << s << "\n";
  94.         } while(next_permutation(s.begin(), s.end()));
  95.     }
  96.  
  97.     return 0;
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement