Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Author : Arif Ahmad
- * Date : 30-08-14
- * Algo : Backtracking + Pruning
- * Difficulty: Medium
- */
- #include <bits/stdc++.h>
- using namespace std;
- #define INF_MAX 2147483647
- #define INF_MIN -2147483648
- #define INF (1 << 30)
- #define EPS 1e-9
- #define PI acos(-1.0)
- #define N 2 + 16
- #define MOD 10000000007
- #define sz(x) (int)(x).size()
- #define all(x) (x).begin(), (x).end()
- #define pb push_back
- #define mp make_pair
- #define ms(x, a) memset((x), (a), sizeof(x))
- #define F first
- #define S second
- #define rep(i,a,b) for(int i=(a); i<(b); i++)
- #define repC(i,x) for(size_t i=0; i<x.size(); i++)
- #define repIT(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
- typedef long long LL;
- typedef pair<int,int> pii;
- typedef vector<int> vi;
- typedef vector<string> vs;
- typedef vector<char> vc;
- typedef vector<bool> vb;
- typedef map<string,int> msi;
- typedef map<int,int> mii;
- typedef map<char,int> mci;
- typedef map<int,string> mis;
- template<class T> T Abs(T x) {return x>0 ? x : -x;}
- template<class T> T Max(T a, T b) {return a>b ? a : b;}
- template<class T> T Min(T a, T b) {return a<b ? a : b;}
- template<class T> T gcd(T a, T b) {return (b ? gcd(b,a%b) : a);}
- bool isVowel(char ch){ch=tolower(ch);return(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u');}
- int n, h;
- char res[20];
- string s;
- bool taken[20];
- void backtrack(int i)
- {
- if(i == n) {
- res[i] = '\0';
- puts(res);
- return;
- }
- for(int j = 0; j < n; j++) if(!taken[j]) {
- taken[j] = true;
- res[i] = s[j];
- backtrack(i+1);
- taken[j] = false;
- while(s[j+1] == s[j]) j++; // pruning
- }
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("in.txt", "r", stdin);
- #endif
- int i, j, k, tc;
- scanf("%d", &tc);
- rep(cn, 1, tc+1)
- {
- scanf("%d%d", &n, &h);
- s = "";
- for(i = 1; i <= (n - h); i++) s += "0";
- for(i = 1; i <= h; i++) s += "1";
- if(cn > 1) cout << "\n";
- //memset(taken, false, sizeof(taken));
- //backtrack(0);
- // 2nd approach - using next_permutation
- do{
- cout << s << "\n";
- } while(next_permutation(s.begin(), s.end()));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement