Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- int st[100], n;
- void Init(int k)
- {
- st[k] = 0;
- }
- int Succesor(int k)
- {
- if(st[k] < n)
- {
- st[k]++;
- return 1;
- }
- return 0;
- }
- int Valid(int k)
- {
- for(int i = 1; i < k; i++)
- {
- if(st[k] == st[i])
- return 0;
- if(abs(double(k-i)) < 1 )
- return 0;
- }
- return 1;
- }
- int Solutie(int k)
- {
- if(k == n+1)
- return 1;
- return 0;
- }
- void Tipar()
- {
- for(int i = 1; i <= n; i++)
- cout << "(" << i << ", " << st[i] << ")";
- cout << "\n";
- }
- void backtrack(int k)
- {
- if(Solutie(k))
- Tipar();
- else
- {
- Init(k);
- while(Succesor(k))
- if(Valid(k))
- backtrack(k+1);
- }
- }
- int main()
- {
- cin >> n;
- backtrack(1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement