Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<fstream>
- using namespace std;
- ofstream fout("matrice.out");
- int st[20], n;
- void tipar(int k) {
- for (int i = 1; i <= k; i++) {
- for (int j = 1; j <= n; j++)
- if (j == st[i])
- fout << 1 << ' ';
- else
- fout << 0 << ' ';
- fout << endl;
- }
- fout << endl;
- }
- int valid(int k) {
- for (int i = 1; i < k; i++)
- if (st[i] == st[k])
- return 0;
- return 1;
- }
- int sol(int k) {
- return k == n;
- }
- void bktr() {
- int k = 1;
- st[k] = 0;
- while (k) {
- if (st[k] < n) {
- st[k]++;
- if (valid(k)) {
- if (sol(k))
- tipar(k);
- else {
- k++;
- st[k] = 0;
- }
- }
- }
- else
- k--;
- }
- }
- int main() {
- cin >> n;
- bktr();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement